5

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,

K Ahir
  • 395
  • 1
  • 6
  • 21
  • thank your for response, by your help, I figured our issue and now I want to accept your asnwer. how can I do it? your answer is as comment right now. – K Ahir Dec 17 '14 at 06:02
  • @Paul Crovella, I can't use foreach with cURL to read data like I used with SimpleXML. Is there any special function that I need to use to read cURL data? Please let me know so I can put complete answer here. – K Ahir Dec 17 '14 at 06:17

2 Answers2

4

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,

Community
  • 1
  • 1
K Ahir
  • 395
  • 1
  • 6
  • 21
  • I had the fopen restriction issue with SimpleXMLElement, and the above cURL did the trick. I'm not sure why the underlying function hasn't been updated, this seems to be problem going back quite a few years. – From Orbonia Mar 29 '20 at 10:30
0

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

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
Raffy Cortez
  • 458
  • 3
  • 8
  • 2
    No, you don't need to use that; in fact, you shouldn't use that. Use `CURLOPT_CAINFO` as demonstrated in [this answer](http://stackoverflow.com/a/316732/1338292). – Ja͢ck Dec 17 '14 at 06:49