php + curl无法设置发布方法 [英] php + curl cannot set post method

查看:86
本文介绍了php + curl无法设置发布方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用php和curl发出发布请求。这是我的代码

I try to make a post request with php and curl. Here is my code

//PHP 5.3.5 and curl: 7.18.2
$ch = curl_init();
if(!empty($save_cookie)){
    curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie);
}else{
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}

curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/post.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, !$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$postResult = curl_exec($ch);

if (curl_errno($ch)) {
    return false;
}

curl_close($ch);
return $postResult;

http://localhost/post.php ,我写

print_r($_SERVER);

卷曲的结果始终是

[REQUEST_METHOD] => GET


推荐答案

删除 CURLOPT_NOBODY 选项,它将起作用。或将其放在 CURLOPT_POST 行上方。

Remove the CURLOPT_NOBODY option and it will work. Or place it above the CURLOPT_POST line.

我想我在尝试获取响应的标头。设置

I think I have encountered this once, when trying to get just the header of a response. Setting

curl_setopt($ch, CURLOPT_NOBODY, true);

有效地指示 curl 发出 HEAD 请求,而不是 POST 请求。我认为没有办法仅从 POST 获取标头(并在收到标头后删除连接)。副作用是,将 CURLOPT_NOBODY 设置为 false 会将请求类型设置为 GET ...

effectively instructs curl to issue a HEAD request, which is not a POST request. I think there is no way to just get the header from a POST (and just drop the connection after receiving the header). As a side effect, setting CURLOPT_NOBODY to false sets the request type to GET...

您真的需要 CURLOPT_NOBODY 标志吗?

这篇关于php + curl无法设置发布方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆