Problem with reading a URL with both fopen, file_get_contents and cURL.

Discussion in 'PHP' started by mNova, Sep 14, 2008.

  1. #1
    I'm writing a script that needs to load a file through HTTP from another server, but the problem is that no matter what method i use, it always causes the script to run for so long that it causes the "max execution time" fatal error.

    The only way I've been able to get some form of result was when i used cURL to spoof the useragent, but i didn't use CURLOPT_RETURNTRANSFER so it echoed out all of the contents of the page. As soon as i used CURLOPT_RETURNTRANSFER, the script would "freeze" when trying to download the page, like it did with all the other methods.

    Here is the code i was using:
    
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_USERAGENT, " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
    //curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    
    PHP:
    Any help would be greatly appreciated.
     
    mNova, Sep 14, 2008 IP
  2. NoNameNoFortune

    NoNameNoFortune Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I use
    			$connection = curl_init();
    		if( $postfields ) {
    			curl_setopt($connection, CURLOPT_POST, 1);
    			curl_setopt($connection, CURLOPT_POSTFIELDS, $postfields);
    		}
    		else
    			curl_setopt($connection, CURLOPT_POST, false);
    		if( $referer )
    			curl_setopt($connection, CURLOPT_REFERER, $referer);
    		if( $cookie )
    			curl_setopt($connection, CURLOPT_COOKIE, $cookie);
    		curl_setopt($connection, CURLOPT_URL, $url);
    		curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 2);
    		curl_setopt($connection, CURLOPT_USERAGENT, $user_agent);
    		curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
    		curl_setopt($connection, CURLOPT_TIMEOUT, 45);						// 45 secs. download page
    		curl_setopt($connection, CURLOPT_CONNECTTIMEOUT, 30);			// 30 server found
    		curl_setopt($connection, CURLOPT_VERBOSE, 1);							// for debugging purposes
    		curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, FALSE);	// this line makes it work under https
    		$result = curl_exec ($connection);
    		curl_close($connection);
    PHP:
    =;-)
     
    NoNameNoFortune, Sep 14, 2008 IP
  3. mNova

    mNova Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I was a bit skeptical that it would work, but it did. Turns out all those extra properties got the server to respond when using REPLYTRANSFER.

    Thanks a heap, i greatly appreciate it.
     
    mNova, Sep 14, 2008 IP