Solved

cURL and PHP GET

Posted on 2014-04-05
10
690 Views
Last Modified: 2014-04-06
Hello,

I am trying to use cURL to send a get request and recieve an XML response. The url I am using is ..

https://securegate.nmp.neuroticmedia.net/WebServices/CatalogService.asmx/artist_export_since_date?APIKey=xxxxxxxxxxxxxxxxxxxxxxxxxxx&Date=04/01/14

Open in new window


when i put it into the browser I get the following response, which is correct..

<SimpleResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://securegate.nmp.neuroticmedia.net/Common/XSDClasses/SimpleResponse.xsd">
<SimpleResponse_data>
<Message>
<ReturnValue>
https://az553772.vo.xxxxxx.net/xxxxxxxxxxxxxx/c2b0e458-e679-4ff8-b464-3e1decf4c1e8_artist.zip
</ReturnValue>
<ResponseCode>0</ResponseCode>
</Message>
</SimpleResponse_data>
</SimpleResponse>

Open in new window


I am trying to use cURL and I keep getting a boo(false) returned.. Any help would be greatly appreciated.

Thanks
0
Comment
Question by:bradley525
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39980462
In order to manage SSL use

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Anyway, use echo curl_error($ch) to know about errors. It's better if you use try..catch block to intercept errors and btw it's ususally better if you post the code here when you ask a question :-) You have posted the url and the expected repoonse, but not the code which fails and we can't not simply guess what's going wrong.
0
 
LVL 2

Expert Comment

by:Robert Silver
ID: 39980503
Okay I will try a good example of this may be available at koders.com now  http://code.ohloh.net

The following was modified code I wrote  from 2005:
This was anonymous-ized  for security and legal reasons but it should get you going

I recall it is essential your request xml in this case the string $headr contains
the xml request message contents and the result shows the response.
In this case a proxy was also in use but I imagine minus the proxy settings should work.
REMEMBER the key is to have an accurate request message to get a proper response and to have full authorization for the transaction as well.

<?
       $URL = "https://remoteserverxyz.xyz.com";

       $headr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
       $headr .="<FDXSubscriptionRequest xmlns:api=\"http://www.remotexyz.com/fsmapi\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"";
       $headr .=" xsi:noNamespaceSchemaLocation=\"FDXSubscriptionRequest.xsd\">";
       $headr .="<RequestHeader>";
       $headr .="<CustomerTransactionIdentifier>String</CustomerTransactionIdentifier>";
       $headr .="<AccountNumber>333496800</AccountNumber>";
       $headr .="</RequestHeader>";
       $headr .="<Contact>";
       $headr .="<PersonName>Perry Rhodan</PersonName>";
       $headr .="<City>New York</City>";
       $headr .="<StateOrProvinceCode>NY</StateOrProvinceCode>";
       $headr .="<PostalCode>11138</PostalCode>";
       $headr .="<CountryCode>US</CountryCode>";
       $headr .="</Address>";
       $headr .="</FDXSubscriptionRequest>";


if (isset($_GET["site"])) { $URL = $_GET["site"]; }
$ch = curl_init();
echo "URL = $URL <br>\n";
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt ($ch, CURLOPT_PROXY,"remoteserverxyz.com:3128");
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 520);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $headr);
$result = curl_exec ($ch);            // Where the req uest was made
                                                      // the $result string contains the response xml
echo "<hr><br>\n";
echo 'Errors: ' . curl_errno($ch) . ' ' . curl_error($ch) . '<br><br>';
echo "<hr><br>\n";
curl_close ($ch);
print "result - $result";
echo "<hr><br>\n";

?>
0
 
LVL 2

Expert Comment

by:Robert Silver
ID: 39980514
As for the SSL part I believe you can specify the host as https: and perhaps specify the SSL_VERIFYPEER as TRUE. Not sure about how to handle a private key there
Try doing a search for  curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
and find that code.  the repository of open source code may be of help in addition.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
LVL 2

Expert Comment

by:Robert Silver
ID: 39980524
Better yet do a site search  in google search on only the site site:curl.haxx.se "CURLOPT_SSL_VERIFYPEER, TRUE" or "using SSL" maybe. This is the home of curl  searching it should get you right to the sample or man page you need.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39980552
Here is a cURL teaching example showing how to make a GET request and visualize error information, if any.  Please read it over and post back if you find any part that you do not understand 100%.

It fails but that is probably because we do not have the correct API key in the post here.

See http://iconoun.com/demo/temp_bradley525.php

<?php // /demo/temp_bradley525.php
error_reporting(E_ALL);


// DEMONSTRATE THE BASICS OF CURL
// SOMETHING LIKE demo/curl_get_example.php?url=http://twitter.com


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

// BUT SINCE IT IS ON MY SERVER, I HAVE HARD-CODED THIS
$url = 'https://securegate.nmp.neuroticmedia.net/WebServices/CatalogService.asmx/artist_export_since_date?APIKey=xxxxxxxxxxxxxxxxxxxxxxxxxxx&Date=04/01/14';

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

// SHOW THE WORK PRODUCT OR BARK OUT 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   );

    // 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 HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        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 XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39980563
I recommend Ray's demo, especially if you are using PHP less than version 5.3.8.  The version of 'libcurl' included in previous versions doesn't do CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER correctly on several sites I checked.
0
 

Author Comment

by:bradley525
ID: 39981195
HI Savant,

I tired to use your sample code above and it did begin to work, but it returned an error, any idea what this error is?

CURL FAIL: https://securegate.nmp.neuroticmedia.net/WebServices/CatalogService.asmx/artist_export_since_date?APIKey=xxxxxxxxxxxxxxxx&Date=04/01/14 TIMEOUT=5, CURL_ERRNO=28array(22) {
  ["url"]=>
  string(155) "https://securegate.nmp.neuroticmedia.net/WebServices/CatalogService.asmx/artist_export_since_date?APIKey=xxxxxxxxxxxxxxxxxx&Date=04/01/14"
  ["content_type"]=>
  NULL
  ["http_code"]=>
  int(0)
  ["header_size"]=>
  int(0)
  ["request_size"]=>
  int(571)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(5.003475)
  ["namelookup_time"]=>
  float(0.186635)
  ["connect_time"]=>
  float(0.210813)
  ["pretransfer_time"]=>
  float(0.281978)
  ["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(0)
  ["starttransfer_time"]=>
  float(0)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
  ["redirect_url"]=>
  string(0) ""
}
<pre>
<strong>https://securegate.nmp.neuroticmedia.net/WebServices/CatalogService.asmx/artist_export_since_date?APIKey=xxxxxxxxxxxxxxxxx&Date=04/01/14</strong>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39981231
Please see http://curl.haxx.se/libcurl/c/libcurl-errors.html

Error code 28 means "timed out."  You might want to increase the time or contact the publisher of the web service to see if the service is up at the moment.

Check the function definition and function call for the value of $timeout.
0
 

Author Closing Comment

by:bradley525
ID: 39981253
Worked perfect, just need to fix the timeout issue..Not an issue with the script.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39981362
Gr8!  Thanks for using EE and thanks for the points, ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question