简体   繁体   中英

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.

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. Not even error 404. My question is, is my curl correct? 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 . 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_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.

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_init can sometimes have this behavior. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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