Solved

cURL and PHP GET

Posted on 2014-04-05
10
687 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

775 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