cURL is driving me crazy please help

Discussion in 'PHP' started by papa_face, Dec 10, 2009.

  1. #1
    Hey,

    I am about to go insane so please can someone come to my aid lol.

    My code:
    function download($url)
    {
     $curl = curl_init();
     
     if($curl)
     {
         if( !curl_setopt($curl, CURLOPT_URL, $url) ) return "FAIL: curl_setopt(CURLOPT_URL)";
    if( !curl_setopt($curl, CURLOPT_HEADER, true) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
    	 if( !curl_setopt($curl, CURLOPT_HTTPHEADER, array("Accept: text/xml,application/xml,application/xhtml+xml,text/html,;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Language: en-us,en;q=0.5", "Connection: keep-alive", "User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.8.1.4) Gecko/20070625 Ubuntu/7.10 (gutsy) Firefox/3.0.0.7", "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Keep-Alive: 300")) ) return "FAIL: curl_setopt(CURLOPT_HTTPHEADER)";
    	 $ret = curl_exec($curl);
         if( !$ret ) return "FAIL: curl_exec()";
         curl_close($curl);
    	 
    	if	(strpos($ret, "Payment Required") > 0)
    		{ 
    		return "Payment is required <br />";
    		}
    	else
    		{
    		return $ret;
    		}
     }
     else return "FAIL: curl_init()";
    }
    Code (markup):
    Sample of cURL output:
    Why would that show when I am asking it to return "Payment is required <br />" if the $ret var contains the string "Payment Required"?

    Any help would be appreciated :)
     
    papa_face, Dec 10, 2009 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    I'm not sure what the script actually does, but an HTTP 402 error literally means that a payment is required.
    http://modemhelp.net/httperrors/402.shtml

    Not much help but I don't have much to go off. How is the function being called? What is the function "download" doing (downloading what file?)?
     
    tguillea, Dec 10, 2009 IP
  3. lunarleviathan

    lunarleviathan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $ret is set to the return value of curl_exec which is a boolean true or false. You are then using strpos to look and see if the string "Payment Required" is in $ret (which it can't be) anywhere other than right at the start.

    So, your function will return $ret, which is equal to true rather than returning the "Payment is required <br />" string, assuming all those curl functions succeed and don't return the error strings.
     
    lunarleviathan, Dec 10, 2009 IP
  4. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    You might be missing CURLOPT_RETURNTRANSFER
     
    javaongsan, Dec 10, 2009 IP
  5. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Maybe:

    if (strpos((string)$ret, "Payment Required") > 0) 
    PHP:
    ?
     
    Wogan, Dec 11, 2009 IP