问题描述
我正在尝试使用 PHP 将 GET 请求中的 OData 参数发送到 RESTful API.对此服务的格式正确的 OData 请求如下所示:
https://myapi.org/endpoint?filter=family_name eq 'Doe'
似乎我应该在发送请求之前将这些变量附加到我的 CURLOPT_URL
的末尾,但 API 服务似乎没有收到 OData.
$ch = curl_init();curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 100);curl_setopt($ch, CURLOPT_HTTPHEADER, array('OSDI-API-Token:xxxxxxxxxxxx'));curl_setopt($ch, CURLOPT_URL, "https://myapi.org/endpoint?filter=family_name eq 'Doe'");$response = curl_exec($ch);curl_close($ch);echo "";打印_r($响应);echo "</pre>";
输出为NULL
.考虑到具有相同标头和相同 Odata URL 的相同请求在 API 浏览器中搜索并找到正确数据,这似乎是一个奇怪的响应.
谁能确认这是否是通过 cURL 请求发送 OData 参数的正确方法?
将 OData 参数直接附加到 CURLOPT_URL
不起作用,因为它没有形成有效的 URL.空格和引号需要转义为 family_name%20eq%20%27Doe%27
或 family_name+eq+%27Doe%27.
更简单的方法是在设置 CURLOPT_URL
之前使用 http_build_query()
将参数附加到 URL:
$ch = curl_init();curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_TIMEOUT, 100);curl_setopt($ch, CURLOPT_HTTPHEADER, array('OSDI-API-Token: xxxxxxxxxx'));$api_request_parameters = array('filter'=>"family_name eq 'Doe'");$api_request_url = "https://myapi.org/endpoint";$api_request_url .= "?".http_build_query($api_request_parameters);$curl_setopt($ch, CURLOPT_URL, $api_request_url);$response = curl_exec($ch);curl_close($ch);
I am trying to send OData parameters in a GET request to a RESTful API using PHP. A properly formatted OData request to this service looks like so:
https://myapi.org/endpoint?filter=family_name eq 'Doe'
It seems like I should just append these variables to the end of my CURLOPT_URL
before sending the request, but the API service doesn't seem to receive the OData.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('OSDI-API-Token:xxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_URL, "https://myapi.org/endpoint?filter=family_name eq 'Doe'");
$response = curl_exec($ch);
curl_close($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
Output is NULL
. This seems like a strange response considering that this same request with identical headers and the same Odata URL searches and finds the correct data in the API's browser.
Can anybody confirm whether or not this is the correct way to send OData parameters through a cURL request?
Appending the OData parameters directly to the CURLOPT_URL
doesn't work, because it doesn't form a valid URL. The spaces and quotes need to be escaped as family_name%20eq%20%27Doe%27
or family_name+eq+%27Doe%27
.
A simpler way would be to use http_build_query()
to attach the parameters to the URL prior to setting CURLOPT_URL
:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('OSDI-API-Token: xxxxxxxxxx'));
$api_request_parameters = array('filter'=>"family_name eq 'Doe'");
$api_request_url = "https://myapi.org/endpoint";
$api_request_url .= "?".http_build_query($api_request_parameters);
$curl_setopt($ch, CURLOPT_URL, $api_request_url);
$response = curl_exec($ch);
curl_close($ch);
这篇关于如何在 PHP cURL 请求中将 OData 发送到 RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!