Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to use cURL instead of SimpleXML_load_file()

Tags:

php

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;

XML data are 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 is not giving 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 you,

like image 559
K Ahir Avatar asked Sep 29 '22 04:09

K Ahir


1 Answers

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

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

Method# 1 : Using simplexml_load_file() - allow_url_fopen should be ON on hosting server for this method to work. This method is working fine both on my local as well as 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, now this method too is working fine both on my local as well as 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;

When using cURL, I was getting no result data so paul-crovella suggested me to check error. so I used below script and I found that I was trying to acess https (SSL certificate) data as also mentioned by Raffy Cortez

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

To resolve this https (SSL certificate) related issue, here is very very helpful link and you can use any of methods mentioned there as per your necessity.

HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK

Thank you,

like image 198
K Ahir Avatar answered Oct 02 '22 16:10

K Ahir