Curl Error Reporting for PHP

This is a follow up question to get some assistance with error reporting for a CURL Request.
Here is the previous question:
Previous Question

Here is my CURL script:

       $url = "http://download.finance.yahoo.com/d/quotes.csv?s=AAWW+ATTU+CJES+CLRO&f=sd1noghl1vpt7t8erdjka2j4j1m3m4w1ba";
	$ch = curl_init(); 
	curl_setopt($ch, CURLOPT_URL, $url); 
	curl_setopt($ch, CURLOPT_HEADER, 0); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	$var = curl_exec($ch); 
	curl_close($ch);
	print 'CURL RESPONSE: <textarea cols=100 rows=25>'.$var.'</textarea>'; die ("at stockPull stop point");

Open in new window


I have gotten this script to work on one server but it doesn't work on a different server that has php configured slightly differently. So I need to find out what about the one server isn't working. I'm having a hard time finding the errors.
Paul KonstanskiProject SpecialistAsked:
Who is Participating?
 
Ray PaseurCommented:
See if adding in some error handling will help.  This is what I often use when I'm not sure what is going on with an external service.  BTW, the script works correctly on my server.
http://iconoun.com/demo/temp_pkonstan1.php
<?php // /demo/temp_pkonstan1.php

/**
 * Demonstrate the basics of a cURL GET-method request.
 * Something like demo/temp_pkonstan1.php?url=http://twitter.com
 */
error_reporting(E_ALL);

// YOU COULD HAVE SOMETHING LIKE THIS
$url = isset($_GET["url"]) ? $_GET["url"] : 'http://twitter.com';

// BUT BECAUSE IT IS ON MY SERVER, I HAVE HARD-CODED THIS
$url = "http://download.finance.yahoo.com/d/quotes.csv?s=AAWW+ATTU+CJES+CLRO&f=sd1noghl1vpt7t8erdjka2j4j1m3m4w1ba";

// TRY THE REMOTE WEB SERVICE
$htm = my_curl($url);

// SHOW THE WORK PRODUCT OR BARK OUT THE ERROR MESSAGES
echo "<pre>";
echo PHP_EOL . '<strong>' . $url . '</strong>' . PHP_EOL;
echo PHP_EOL . htmlentities($htm);
echo PHP_EOL;


// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );
    curl_setopt( $curl, CURLOPT_VERBOSE,        TRUE   );
    curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE   );


    // IF USING SSL, THESE MAY BE IMPORTANT
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, FALSE  );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE  );
    curl_setopt( $curl, CURLOPT_SSLVERSION,     3      );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE
    if ($htm === FALSE)
    {
        // VISUALIZE ERROR MESSAGES
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN JSON / XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
You may need to change this line.
curl_setopt( $curl, CURLOPT_SSLVERSION,     3      );

Open in new window

That selects SSLv3 which has been disabled on many servers.  I had to change some of my curl routines to '1' to select TLSv1 because '3' stopped working.  So far my Paypal routines are working fine with '0' or autoselect.

From this page http://php.net/manual/en/function.curl-setopt.php :
Note:
Your best bet is to not set this and let it use the default. Setting it to 2 or 3 is very dangerous given the known vulnerabilities in SSLv2 and SSLv3.
0
 
Ray PaseurCommented:
@Dave: I don't think that is applicable here, because the protocol is http, not https.  I think cURL will just ignore that setting.  Please let me know if I'm off base.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Dave BaldwinFixer of ProblemsCommented:
Technically you are correct and practically, it is now bad advice.  If you left that line out completely, it would still work fine.
0
 
Ray PaseurCommented:
Good point, Dave - it's an old example.  I'll update it.
0
 
Paul KonstanskiProject SpecialistAuthor Commented:
Here is the error message I am getting. It gives a CURL_28 timeout error.

CURL FAIL: http://download.finance.yahoo.com/d/quotes.csv?s=AAWW+ATTU+CJES+CLRO&f=sd1noghl1vpt7t8erdjka2j4j1m3m4w1ba TIMEOUT=5, CURL_ERRNO=28array(22) { ["url"]=> string(105) "http://download.finance.yahoo.com/d/quotes.csv?s=AAWW+ATTU+CJES+CLRO&f=sd1noghl1vpt7t8erdjka2j4j1m3m4w1ba" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(5.004843) ["namelookup_time"]=> float(0.164826) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }
http://download.finance.yahoo.com/d/quotes.csv?s=AAWW+ATTU+CJES+CLRO&f=sd1noghl1vpt7t8erdjka2j4j1m3m4w1ba

Open in new window


I've been told by my hosting service that it has to be some setting within my php. Any ideas what setting might need to be checked?
0
 
Ray PaseurCommented:
cURL #28:
http://curl.haxx.se/mail/curlphp-2004-03/0008.html

Who is your hosting service?  That sounds like they don't really understand the issue.

Output from the script on my server:
http://download.finance.yahoo.com/d/quotes.csv?s=AAWW+ATTU+CJES+CLRO&f=sd1noghl1vpt7t8erdjka2j4j1m3m4w1ba

"AAWW","5/6/2015","Atlas Air Worldwide Holdings",55.55,54.89,56.37,55.85,668593,55.24,N/A,57.30,4.25,13.14,N/A,31.09,56.37,288575,344.09M,1.39B,45.29,44.93,N/A,54.10,56.43
"ATTU","5/6/2015","Attunity Ltd.",11.93,11.70,12.15,11.78,43519,12.00,N/A,16.50,-0.11,N/A,N/A,5.83,12.53,42008,1.50M,188.82M,10.36,9.81,N/A,N/A,11.76
"CJES","5/6/2015","C&J Energy Services, Ltd. Commo",17.64,16.91,18.05,17.24,2490939,17.30,N/A,17.38,1.22,14.13,N/A,9.11,34.93,1919600,231.87M,930.20M,13.75,14.12,N/A,17.27,17.28
"CLRO","5/6/2015","ClearOne, Inc.",13.470,12.800,13.470,13.015,35738,13.280,N/A,15.250,0.580,22.440,0.100,7.470,13.490,19493,10.53M,118.58M,11.067,10.183,N/A,N/A,99.000

Open in new window

This works the same way with or without the SSL_VERSION = 3, but if that is considered less than best-practices, I would recommend omitting it.
0
 
Paul KonstanskiProject SpecialistAuthor Commented:
I still have not gotten an answer from the hosting service regarding what they did, but they performed some action that cleaned this up.

All is working fine now. Thanks everybody for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.