0

I'm calling a URL of this kind:

http://localhost:9910/app/check?name=alvaro&test=true

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?

13
  • You echo nothing. What did you expect? Commented Jan 22, 2016 at 18:13
  • @u_mulder my point is, the server times out on curl_exec... never coming back. I don't echo anything in this example because it is not necessary for my question. Commented Jan 22, 2016 at 18:14
  • try to add curl_setopt($curl, CURLOPT_PORT, 9910); Commented Jan 22, 2016 at 18:14
  • @Slam nothing. Same. Commented Jan 22, 2016 at 18:15
  • 1
    change the print_r in the last line to echo and are you sure what you're connecting to requires the client to send the 'Content-Type: application/json' and 'Content-Length: nnn' headers? Because if they're supposed to be response headers, then don't send those headers as part of the request. Commented Jan 22, 2016 at 18:19

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.