php – 想要使用cURL而不是SimpleXML_load_file()

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 想要使用cURL而不是SimpleXML_load_file()脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
使用下面的脚本我可以成功解析API数据.

$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;@H_406_8@ 
 

XML数据是这样的:

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

但我想使用cURL而不是simplexml_load_file.所以我使用下面的脚本,但它没有提供任何结果数据.

$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;

请让我知道我错过了什么或做错了什么.

谢谢,

解决方法

好的,这是我的完整答案,希望对其他人有用.

我使用了两种方法从特定链接读取XML数据.

方法#1:使用simplexml_load_file() – allow_url_foPEn应该在托管服务器上为ON,以使此方法有效.这种方法在我的本地服务器和实际服务器上都运行良好.

$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;

方法#2:使用cURL – 按照这里的建议进行操作后,现在这方法在我的本地服务器和实际服务器上都运行良好.

$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,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;

使用cURL时,我没有得到结果数据,因此paul-crovella建议我检查错误.所以我使用下面的脚本,我发现我正在尝试访问Raffy Cortez提到的https(SSL证书)数据

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

解决此https(SSL证书)相关问题,这里是非常有用的链接,您可以根据需要使用其中提到的任何方法.

HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed,CA is OK

谢谢,

脚本宝典总结

以上是脚本宝典为你收集整理的php – 想要使用cURL而不是SimpleXML_load_file()全部内容,希望文章能够帮你解决php – 想要使用cURL而不是SimpleXML_load_file()所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。