• <i id='W7H1h'><tr id='W7H1h'><dt id='W7H1h'><q id='W7H1h'><span id='W7H1h'><b id='W7H1h'><form id='W7H1h'><ins id='W7H1h'></ins><ul id='W7H1h'></ul><sub id='W7H1h'></sub></form><legend id='W7H1h'></legend><bdo id='W7H1h'><pre id='W7H1h'><center id='W7H1h'></center></pre></bdo></b><th id='W7H1h'></th></span></q></dt></tr></i><div id='W7H1h'><tfoot id='W7H1h'></tfoot><dl id='W7H1h'><fieldset id='W7H1h'></fieldset></dl></div>

      <small id='W7H1h'></small><noframes id='W7H1h'>

        • <bdo id='W7H1h'></bdo><ul id='W7H1h'></ul>
        <legend id='W7H1h'><style id='W7H1h'><dir id='W7H1h'><q id='W7H1h'></q></dir></style></legend><tfoot id='W7H1h'></tfoot>

        如何通过 PHP 访问 RESTful API

        时间:2023-10-13
        • <tfoot id='U2lzg'></tfoot>
            <bdo id='U2lzg'></bdo><ul id='U2lzg'></ul>
          • <small id='U2lzg'></small><noframes id='U2lzg'>

          • <legend id='U2lzg'><style id='U2lzg'><dir id='U2lzg'><q id='U2lzg'></q></dir></style></legend>

                <i id='U2lzg'><tr id='U2lzg'><dt id='U2lzg'><q id='U2lzg'><span id='U2lzg'><b id='U2lzg'><form id='U2lzg'><ins id='U2lzg'></ins><ul id='U2lzg'></ul><sub id='U2lzg'></sub></form><legend id='U2lzg'></legend><bdo id='U2lzg'><pre id='U2lzg'><center id='U2lzg'></center></pre></bdo></b><th id='U2lzg'></th></span></q></dt></tr></i><div id='U2lzg'><tfoot id='U2lzg'></tfoot><dl id='U2lzg'><fieldset id='U2lzg'></fieldset></dl></div>

                    <tbody id='U2lzg'></tbody>
                  本文介绍了如何通过 PHP 访问 RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 PHP 以及使用 RESTful API 的整个过程非常陌生.我现在想要做的就是成功地向OpenStreetMap API.

                  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.

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

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

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

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

                  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.

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

                  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.

                  感谢您的帮助!

                  推荐答案

                  使用 curl.见 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" => 'lorna@example.com',
                          );
                     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);

                  这篇关于如何通过 PHP 访问 RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用PHP抓取URL参数? 下一篇:将对象从一页移动到另一页?

                  相关文章

                1. <legend id='YmVRN'><style id='YmVRN'><dir id='YmVRN'><q id='YmVRN'></q></dir></style></legend>
                  <tfoot id='YmVRN'></tfoot>
                  • <bdo id='YmVRN'></bdo><ul id='YmVRN'></ul>

                    <small id='YmVRN'></small><noframes id='YmVRN'>

                    <i id='YmVRN'><tr id='YmVRN'><dt id='YmVRN'><q id='YmVRN'><span id='YmVRN'><b id='YmVRN'><form id='YmVRN'><ins id='YmVRN'></ins><ul id='YmVRN'></ul><sub id='YmVRN'></sub></form><legend id='YmVRN'></legend><bdo id='YmVRN'><pre id='YmVRN'><center id='YmVRN'></center></pre></bdo></b><th id='YmVRN'></th></span></q></dt></tr></i><div id='YmVRN'><tfoot id='YmVRN'></tfoot><dl id='YmVRN'><fieldset id='YmVRN'></fieldset></dl></div>