Reputation: 41605
I'm calling a URL of this kind:
Accessing through the browser URL I get the correct result. And the same when using the command line with curl
, it retrieves the correct information:
C:\>curl "http://localhost:9910/boxreload/check?name=alvaro&test=true" -i
HTTP/1.1 200 OK
Content-type: application/json; charset=UTF-8
Content-length: 203
Server: Restlet-Framework/2.3.3
Accept-ranges: bytes
Date: Fri, 22 Jan 2016 18:01:30 GMT
{"restul": "true"}
But when doing it with PHP it never comes back from the curl_exec
and the server times out after 30 seconds.
$stringData = http_build_query($data);
$url = sprintf("%s?%s", $url, $stringData);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;',
'Content-Length: ' . strlen($stringData))
);
// $url is "'http://localhost:9910/boxreload/check?name=alvaro&test=true'"
curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//never comes back from this call (server times out)
$curl_response = curl_exec($curl);
if(curl_response === FALSE){
echo "error en CURL";
echo curl_error($curl);
}
curl_close($curl);
//never reaches this point
print_r($curl_response);
Why is it? Am I doing anything wrong?
Upvotes: 0
Views: 1168
Reputation: 165
You forgot to do curl_init
$curl = curl_init();
and your if statement has a missing $ in front of the variable. check this code:
$data = [];
$stringData = http_build_query($data);
$url = sprintf("%s?%s", "http://google.ro/", $stringData);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;',
'Content-Length: ' . strlen($stringData))
);
// $url is "'http://localhost:9910/boxreload/check?name=alvaro&test=true'"
curl_setopt($curl, CURLOPT_URL, $url );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
//never comes back from this call (server times out)
$curl_response = curl_exec($curl);
if($curl_response === FALSE){
echo "error en CURL";
echo curl_error($curl);
}
curl_close($curl);
//never reaches this point
print_r($curl_response);
Upvotes: 0
Reputation: 2092
As suggested, I'm turning my comment into an answer.
change the print_r in the last line to echo so that you can see the raw string output.
This code fragment:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json;',
'Content-Length: ' . strlen($stringData))
);
means you're sending these headers to the server when making the request:
Content-Type: application/json
Content-Length: nnn
where nnn is an integer representing the length of $stringData
.
Just make sure the proper headers are sent when making the request or you will likely receive undesired results. It is unusual to specify the content-type and content-length as headers being passed from client to server. It should be the other way around (where server sends to the client the content-type and content-length header).
Upvotes: 2