Want to use cURL instead of SimpleXML_load_file ()

Using below script I can parse API data successfully.

$xml_report_daily=simplexml_load_file("https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25");

foreach ($xml_report_daily as $report_daily):
    $trans_id=$report_daily->TRANSID;
    $trans_id=$report_daily->MID;
    $trans_id=$report_daily->EXT;
    $trans_id=$report_daily->USER;
endforeach;

      

The XML data looks something like this:

<DATABASE>
    <RECORD>
        <TRANSID>1348818</TRANSID>
        <MID/>
        <EXT>0</EXT>
        <USER>00012345</USER>
    </RECORD>
    .
    .
    .
    so on...
</DATABASE>

      

But I want to use cURL instead of simplexml_load_file. So I used below script but it doesn't give any result data.

$url = "https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25"; 
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
echo $xml;

      

Please let me know what I am missing or doing wrong.

Thank,

+3


source to share


2 answers


Ok, here is my complete answer and hope it will be helpful to others.

I have used 2 methods to read XML data from a specific link.

Method # 1: Using simplexml_load_file () - allow_url_fopen must be enabled on the hosting server for this method to work. This method works fine on both my local and actual server.

$xml_report_daily=simplexml_load_file("https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25");

foreach ($xml_report_daily as $report_daily):
    $trans_id=$report_daily->TRANSID;
    $m_id=$report_daily->MID;
    $ext_id=$report_daily->EXT;
    $user_id=$report_daily->USER;
    echo $trans_id."&nbsp;".$m_id."&nbsp;".$ext_id."&nbsp;".$user_id."<br/>";
endforeach;

      

Method # 2: Using cURL - After doing as suggested here, this method now also works fine on both my local and actual server.

$url = "https://api.sitename.com/api/reports/api_get.asp?User=00012345&Key=abcdefghijklmnop&fromDate=11/12/2014&toDate=12/12/2014&mid=25"; 
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);

$xml_report_daily = simplexml_load_string($xml);

foreach ($xml_report_daily as $report_daily):
    $trans_id=$report_daily->TRANSID;
    $m_id=$report_daily->MID;
    $ext_id=$report_daily->EXT;
    $user_id=$report_daily->USER;
    echo $trans_id."&nbsp;".$m_id."&nbsp;".$ext_id."&nbsp;".$user_id."<br/>";
endforeach;

      



I was not getting any result data when using cURL, so paul-crovella suggested that I check the error. so i used below script and i found myself trying to get https data (SSL certificate) also mentioned by Raffy Cortez

if(curl_exec($ch) === false)
    { echo 'Curl error: ' . curl_error($ch); }
else
    { echo 'Operation completed without any errors'; }

      

To fix this https (SSL certificate) related issue here is a very helpful link and you can use any of the methods mentioned there depending on your need.

HTTPS and SSL3_GET_SERVER_CERTIFICATE: Certificate verification failed, CA ok

Thank,

+1


source


You are calling https url in your cURL, you need to use



curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

      

0


source







All Articles