我怎样才能获得和处理从PHP API响应 [英] How can I get and process a response from a PHP API

查看:136
本文介绍了我怎样才能获得和处理从PHP API响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我会努力保持这个短。我做一个简单的PHP API可以通过AJAX通过JavaScript或JQuery的做的请求,但是我要让使用PHP请求。什么是做到这一点的最好方法是什么?使用的file_get_contents()函数或CURL?如果是的话我怎么做卷曲通过的API需要我用GET POST没有。我也知道响应是XML,我怎么再处理响应一旦回来?

这问题可能已经问了很多次,审查了很多的问题和答案然而,当他们并不是专门针对我的这一个,所以请与请看到这个链接我可以保证它没有获得答案的需求ŧ回答充满了一个问题,他们中的很多正在无论从Facebook的API或其他API请求没有做什么我使用的API一样。


解决方案

 函数的getXML()
{ $ CH = curl_init();
 curl_setopt($ CH,CURLOPT_URLhttp://127.0.0.1/index.html);
 curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
 curl_setopt($ CH,CURLOPT_USERPWD,用户:密码);
 curl_setopt($ CH,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
 $解析度= curl_exec($ CH); 返回$资源;}    $ xml_data =的getXML();    $ DOC =新的DOMDocument();
    $ doc->的loadXML($ xml_data);    $ WMS = $ doc->的getElementsByTagName('WowzaMediaServer');
    $ wmstotalactive = $ wms->项(0) - >的getElementsByTagName(ConnectionsCurrent) - >项(0) - >的nodeValue;
    $ wmstotaloutbytes = $ wms->项(0) - >的getElementsByTagName(MessagesOutBytesRate) - >项(0) - >的nodeValue;

让你从XML从ConnectionsCurrent键和MessagesOutBytesRate值提取

如果您的链接并不需要权威性删除:

curl_setopt($ CH,CURLOPT_USERPWD,用户:密码);

curl_setopt($ CH,CURLOPT_HTTPAUTH,CURLAUTH_ANY);

Ok I will try and keep this short. I'm making a request to a simple PHP API which can be done by AJAX through Javascript or JQuery, however I want to make the request using PHP. What is the best way to do this? Using the file_get_contents() function or CURL? If so how do I do it through CURL as the API requires I use GET not POST. Also the response I know is in XML, how do I then process the response once it comes back?

This question may have been asked many times, however when reviewing a lot of the questions and answers they are not specific to my needs on this one so please no answers with "Please see this link" as I can guarantee it won't answer the question in full as a lot of them are making requests from either Facebook API or another API that does not do what the API I am using does.

解决方案

function  getXML()
{

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/index.html");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_USERPWD, "user:password");
 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
 $res = curl_exec($ch);

 return  $res;

}

    $xml_data = getXML();

    $doc = new DOMDocument();
    $doc->loadXML($xml_data);

    $wms = $doc->getElementsByTagName('WowzaMediaServer');


    $wmstotalactive = $wms->item(0)->getElementsByTagName("ConnectionsCurrent")->item(0)->nodeValue;
    $wmstotaloutbytes = $wms->item(0)->getElementsByTagName("MessagesOutBytesRate")->item(0)->nodeValue;

so you extracted from xml the value from ConnectionsCurrent key and MessagesOutBytesRate .

If your link does not need to auth remove :

curl_setopt($ch, CURLOPT_USERPWD, "user:password");

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

这篇关于我怎样才能获得和处理从PHP API响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆