我是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假设为变量,而其他我不知道该怎么做
答案 0 :(得分:13)
使用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);
答案 1 :(得分:1)
希望这会对你有所帮助。
$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;
}
}