Page 1 of 1

cURL works but file_get_contents does not

Posted: Tue Sep 22, 2009 4:52 pm
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

Re: cURL works but file_get_contents does not

Posted: Tue Sep 22, 2009 4:56 pm
by jackpf
Just a guess, but do you have allow_url_fopen turned on in php.ini?

Re: cURL works but file_get_contents does not

Posted: Tue Sep 22, 2009 5:31 pm
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",