Restful 来电 PHP

Restful call in PHP

我对 PHP 很陌生。我想在 PHP 中调用 restful api。我在 jQuery 中做同样的事情,如下

var url;
url = "https://www.example.com/_api/lists/getbytitle('Stations')/items?$select=CityCode,CityNameEN&$filter=Active eq 'Yes'&$orderby=CityNameEN";
        $.ajax({
            url: url,
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            async: false,
            success: function (data) {
                $.each(data.d.results, function (i, item) {

                });
            }
        });

如何在 PHP 中执行相同操作?我有两个困难。首先在 URL 中有 $ 符号,PHP 将其假设为变量,其他我不知道该怎么做

使用 curl 发送 HTTP 请求。

<?php
$url='https://www.kuwaitairways.com/_api/web/lists/getbytitle(\'Stations\')/items?$select=OfficeId&$filter=CityCode%20eq%20%27KWI%27';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $result = curl_exec($ch);

希望对您有所帮助。

$url="https://www.kuwaitairways.com/_api/web/lists/getbytitle('Stations')/items?";
$url .= "$select=OfficeId&$filter=".urlencode("CityCode eq 'KWI'");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $result = curl_exec($ch);

检查下面的代码以获取 d:OfficeId 节点值

$office_id = "";
$xml=new DOMDocument;
$xml->loadXML($result);
$content_node = $xml->getElementsByTagName('content')->item(0);
foreach ($content_node->childNodes as $properties_node) {
    foreach ($properties_node->childNodes as $office_node) {
        $office_id = $office_node->nodeValue;
    }   
}