如何通过 PHP 访问 RESTful API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7999323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:44:59  来源:igfitidea点击:

How to access RESTful API via PHP

phprestgetopenstreetmap

提问by Matthias Loeks

I'm pretty new to PHP and the whole thing of working with RESTful APIs. All I want to do at the moment is successfully issue a plain HTTP GET request to the OpenStreetMap API.

我对 PHP 以及使用 RESTful API 的整个过程很陌生。我现在想要做的就是成功地向OpenStreetMap API发出一个普通的 HTTP GET 请求。

I am using the simple PHP REST client by tcdentand I basically understand it's functionality. My example code for getting the current Changesets in OSM is:

我正在使用tcdent简单 PHP REST 客户端,我基本上了解它的功能。我在 OSM 中获取当前变更集的示例代码是:

<?php
 include("restclient.php");

 $api = new RestClient(array(
     'base_url' => "http://api.openstreetmaps.org/", 
     'format' => "xml")
 );
 $result = $api->get("api/0.6/changesets");

 if($result->info->http_code < 400) {         
     echo "success:<br/><br/>";         
 } else {
     echo "failed:<br/><br/>";
 }
 echo $result->response;
?>

When I enter the URL "http://api.openstreetmaps.org/api/0.6/changesets" in the browser, it delivers the XML file. However, through this PHP code it returns the OSM 404 File not Found page.

当我在浏览器中输入 URL“http://api.openstreetmaps.org/api/0.6/changesets”时,它会提供 XML 文件。但是,通过此 PHP 代码,它返回 OSM 404 File not Found 页面。

I guess this is a rather stupid PHP-newbie question but I cannot see what I am missing, since I do not know much (yet) about all these client-server side processes etc.

我想这是一个相当愚蠢的 PHP 新手问题,但我看不出我遗漏了什么,因为我(还)对所有这些客户端-服务器端进程等知之甚少。

Thanks for your help!

谢谢你的帮助!

回答by Martin Stein

Use curl. See http://www.lornajane.net/posts/2008/using-curl-and-php-to-talk-to-a-rest-service

使用卷曲。见http://www.lornajane.net/posts/2008/using-curl-and-php-to-talk-to-a-rest-service

   $service_url = 'http://example.com/rest/user/';
   $curl = curl_init($service_url);
   $curl_post_data = array(
        "user_id" => 42,
        "emailaddress" => '[email protected]',
        );
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_POST, true);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
   $curl_response = curl_exec($curl);
   curl_close($curl);

$xml = new SimpleXMLElement($curl_response);

$xml = new SimpleXMLElement($curl_response);

回答by Matthias Loeks

OK, the problem was apparently the 'format' => "xml" specification. Without it and with the help of SimpleXMLElement (thanks Martin), I am now getting the XML data loaded properly:

好的,问题显然是“格式”=>“xml”规范。没有它并在 SimpleXMLElement 的帮助下(感谢 Martin),我现在可以正确加载 XML 数据:

<?php
   include("restclient.php");
   $api = new RestClient(); 
   $result = $api->get("http://api.openstreetmap.org/api/capabilities");
   $code = $result->info->http_code;
   if($code == 200) {
       $xml = new SimpleXMLElement($result->response);
       echo "Loaded XML, root element: ".$xml->getName();
   } else {
       echo "GET failed, error code: ".$code;
   }
?>

Although this is not a very flexible approach since it works only for XML responses, it is enough for the moment and a good point to start with the OSM API.

尽管这不是一种非常灵活的方法,因为它仅适用于 XML 响应,但目前已经足够,并且是从 OSM API 开始的好方法。

Thanks for your help!

谢谢你的帮助!