0

I'm going crazy with this. I read some other question about it, but never found an answer.

My problem is: I'm trying to create a PHP Curl request (GET), and the request return false if I set the url from a variable.

Here below my code:

Working code:

$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, "www.google.com");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_PORT, 80);
$output = curl_exec ($ch);
echo json_encode($output);

Not working code:

$url = "www.google.com";
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_PORT, 80);
$output = curl_exec ($ch);
echo json_encode($output);

Someone could help me with this? Thank you.

The_Rkp
  • 11
  • 6
  • 2
    Missing `=` might be the problem, I guess, if it's your real code. It should be `$url = "www.google.com";` – Rajdeep Paul Feb 05 '17 at 19:37
  • `$url = "google.com"`, where is aasignment operator (=) – BetaDev Feb 05 '17 at 19:37
  • is your error_reporting on? Don't you get any error message? Always set error reporting on during development. – TIGER Feb 05 '17 at 19:41
  • Sorry, of course there is the "=" it is a wrong cut and paste. – The_Rkp Feb 05 '17 at 19:47
  • No, no getting error :-( – The_Rkp Feb 05 '17 at 19:48
  • I can't replicate your issue, it works fine either way for me. If you must save the $url as a variable, have you tried passing it to the curl_init() function? Maybe that will work? – djs Feb 05 '17 at 19:59
  • Tried also that way (passing to curl_init) same issue. I can't figure it out, it's very strange. – The_Rkp Feb 05 '17 at 20:02
  • Look here, maybe you'll have an answer http://stackoverflow.com/questions/8865241/bash-curl-and-variable-in-the-middle-of-the-url and http://stackoverflow.com/questions/13341955/how-to-pass-a-variable-in-a-curl-command-in-shell-scripting. also should you add the protocol (http) to the url? – Louis Loudog Trottier Feb 05 '17 at 21:15
  • what does this code say? `var_dump('does these look the same now? '.($url==='www.google.com'?'yup':'no, they do not'),"www.google.com",$url);` – hanshenrik Feb 05 '17 at 21:44
  • Guys thank you for your comments, at the end I figured it out. The curl request was inside a function but (for some mystical reason), the $url variable was outside, so the init was unable to see the variable content. Sorry for my stupid mistake – The_Rkp Feb 06 '17 at 07:20

0 Answers0