简体   繁体   English

cURL PHP GET未发送

[英]cURL PHP GET not sending

I am trying to send a CURL GET data to a certain website, but it seems it never goes through. 我正在尝试将CURL GET数据发送到某个网站,但似乎从未成功。

It needs to be something like the following: 它需要类似于以下内容:

GET /webservices/ssl/something.asmx HTTP/1.1
Host: http://www.thewebsite.com

I have tried the following, but it seems it doesnt go through: 我尝试了以下方法,但似乎没有通过:

$url = "http://www.thewebsite.com/webservices/ssl/something.asmx";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.thewebsite.com'));
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($result);
print_r($info);

The print_r($result) prints nothing. print_r($result)打印任何内容。 Not even error 404. My question is, is my curl correct? 甚至没有错误404。我的问题是,我的卷曲是否正确? is there something that I miss? 有什么我想念的吗? If not, could you please help me point out which one is the error and how to fix it? 如果没有,您能帮我指出哪个错误以及如何解决吗?

This type of behavior usually happens when the remote server refuse to serve to request from agents like curl , wget . 当远程服务器拒绝响应来自curlwget类的代理的请求时,通常会发生这种行为。 So we have to spoof the agent header. 因此,我们必须欺骗代理标头。

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");

you don't need the following headers because curl handles that on its own. 您不需要以下标头,因为curl可自行处理。

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.thewebsite.com'));
curl_setopt($ch, CURLOPT_HTTP_VERSION, 1.1);

Apart from these if your site requires cookies Don't forget to include storage for cookies. 除此之外,如果您的站点需要Cookie,请不要忘记包括cookie的存储。

curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

lastly if curl_exec returns FALSE check curl_error() for more information. 最后,如果curl_exec返回FALSE请检查curl_error()以获取更多信息。

curl_init can sometimes have this behavior. curl_init有时可能会出现这种现象。 Try adding a trailing slash '/' after the url. 尝试在网址后添加斜杠“ /”。

Moreover, take into account that the function curl_init should return a FALSE if there are errors or you have an empty result like in your case. 此外,请考虑到函数curl_init在出现错误或结果为空(如您的情况)时应返回FALSE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM