2

I have been searching for answer here about 404, but I'm not lucky to solve my problem. I have the same problem in previous post. I already copy and paste their answer but no luck again.

here's my code

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$raw_data = curl_exec($ch);
curl_close($ch);

var_dump($raw_data);

I don't know if I have a right code in curl. But if I paste the url in to a browser, I get the result. Is there something that I need to configure or set in my server? I don't know what the cause of this. Do I need to involve server personnel to check on this? Hoping for your advice here. I'm new of this.

user3818576
  • 2,979
  • 8
  • 37
  • 62
  • please post the contents of `$url` and the output of the `var_dump` – Alex Andrei Sep 30 '15 at 08:45
  • sorry the url is private. I can't post it. – user3818576 Sep 30 '15 at 08:51
  • 1
    Try adding `curl_setopt($ch, CURLOPT_VERBOSE, true);` to see what's happening under the hood. – Another Code Sep 30 '15 at 08:52
  • I get the same issue. but the problem I think is that url only for staging work. no external IP added. Is this the right cause of my problem? – user3818576 Sep 30 '15 at 08:58
  • `CURLOPT_VERBOSE` is not a solution, but adding it makes cURL output debug information so you can diagnose the issue. Particularly interesting are the request headers, try comparing them with the headers your browser sends (you can usually view this with integrated developer tools or extensions like Firebug). – Another Code Sep 30 '15 at 09:38
  • Hi. I already get the answer of my post. It needs an external IP Address to work and pass the data. the code is now fine. thanks for your advice – user3818576 Sep 30 '15 at 09:47
  • Hi user3818576! I am having same problem, but I don't understand how you solved it. What does "it needs an external IP address" mean there? And how did you practically solve your case? Thanks! – user2708647 Aug 30 '18 at 21:49

1 Answers1

2

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); prevents curl from following redirects. Therefore, if you URL is redirected (from http to https, domain.com to www.domain.com, etc.), it won't work.

The browser, however, does redirect you. Try changing this line to curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);, it should help!

Adrien
  • 1,929
  • 1
  • 13
  • 23
  • I get the same result. 404 not found. but I will ask my admin if the url is redirected to something. thank for your advice. – user3818576 Sep 30 '15 at 08:50
  • Does your URL contain "http://" or "https://"? Browsers automatically add it, but curl does not – Adrien Sep 30 '15 at 08:55
  • The problem I think is that url only for staging work. no external IP added. Is this the right cause of my problem? – user3818576 Sep 30 '15 at 08:56
  • Hi. I already get the answer of my post. It needs an external IP Address to work and pass the data. the code is now fine. thanks for your advice – user3818576 Sep 30 '15 at 09:46
  • @Adrien if there is any follow-location happens, then it would return `301` or `302` response code instead of `404`. This implies your answer is incorrect. – Sabuj Hassan Sep 30 '15 at 10:11