cURL works but file_get_contents does not

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeff00seattle
Forum Commoner
Posts: 66
Joined: Sat Feb 28, 2009 3:27 pm

cURL works but file_get_contents does not

Post by jeff00seattle »

The following cURL code works great, but note that I did need to use a proxy to make this call work:

Code: Select all

 $search_url = "http://www.google.com/";
 
  if (function_exists("curl_init")) {
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_PROXY, "itgproxy:80");
    curl_setopt ($ch, CURLOPT_URL, $search_url); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0); 
    $response = curl_exec($ch); 
    curl_close($ch);
 
    var_dump( $response );
  }
But I want to understand why file_get_contents() fails:

Code: Select all

 $search_url = "http://www.google.com/";
  if (function_exists("file_get_contents")) {
    
    $opts = array(  
      'http'=>array(    
          'method'=>"GET",    
          'proxy'=>"itgproxy:80",
        )
    );
    $context = stream_context_create($opts);
    $response = file_get_contents($search_url, false, $context);
    var_dump( $response );
  }
The error warning is as follows:
PHP Warning: file_get_contents(http://www.google.com/) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: HTTP request failed! HTTP/1.1 502 Proxy Error ( The Uniform Resource Locator (URL) does not use a recognized protocol. Either the protocol is not s in C:\\...\\NetworkTest.php on line 26
1. Why would file_get_contents() not recognize http?
2. What does it mean by Either the protocol is not s in <file name>

Thanks

Jeff in Seattle
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: cURL works but file_get_contents does not

Post by jackpf »

Just a guess, but do you have allow_url_fopen turned on in php.ini?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: cURL works but file_get_contents does not

Post by John Cartwright »

It's all in the error Mr. Watson.

502 Proxy Error ( The Uniform Resource Locator (URL) does not use a recognized protocol.

You did not specify the protocol, i.e.,

Code: Select all

'proxy'=>"tcp://itgproxy:80",
Post Reply