5

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?

1
  • 1
    In OData, system query options begin with a dollar sign, so you should be writing https://myapi.org/endpoint?$filter=family_name eq 'Doe'.
    – lencharest
    Feb 20, 2016 at 5:28

2 Answers 2

14

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);
0
0

I know that this question has been closed for a while but just to complement the answer with more points above, you need to declare '$' in the filter key if you are going to use http_build_query (), for example:

$api_request_parameters = array ('$filter' => "family_name eq 'Doe'");
$api_request_url. = "?". http_build_query ($api_request_parameters);

If you declare without '$' in filter, the filter will not work correctly and the return of this will be all records and in some cases it may return an error.

If you choose not to use http_build_query () you need to escape all spaces, for example, let's say the request is this:

https://myapi.org/endpoint?filter=family_name eq 'Doe test 1234'

The parameters would look like this:

https://myapi.org/endpoint?filter=family_name+eq+%27Doe%20test%201234%27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.