Problems when defining url string whose query string parameter contains the dollar ($) character

Asked

Viewed 306 times

3

I am using a WS API in JSON

that the URL is

https://api.movidesk.com/public/v1/persons?token=52ee6ca5-8639-422b-bafe-470013c11176&$filter=profileType eq 2

which is the example in which the API documentation passes. when game in browser gets

https://api.movidesk.com/public/v1/persons?token=52ee6ca5-8639-422b-bafe-470013c11176&$filter=profileType%20eq%202

But the information appears

My problem is when I will use the PHP API

Example

 <?php $json = file_get_contents("https://api.movidesk.com/public/v1/persons?token=52ee6ca5-8639-422b-bafe-470013c11176&$filter=profileType%20eq%202");
      $cliente = json_decode($json);

returns me the following error

Notice: Undefined variable: filter in C: xampp htdocs Maps json.php on line 54

Warning: file_get_contents(https://api.movidesk.com/public/v1/persons?token=52ee6ca5-8639-422b-bafe-470013c11176&$filter=profiletype%20eq%202): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C: xampp htdocs Maps json.php on line 54

  • 1

    Try to remove the dollar sign from &$filter or slip &\$filter

1 answer

2


From what I understand, the url parameter has a value called $filter.

In the section where you try to make the request with file_get_contents, you are using double quotes. When you do this, PHP interprets values that start with $ as the variable value.

This happened because you are using double quotes. Just use single quotes that will solve the problem.

$json = file_get_contents('https://api.movidesk.com/public/v1/persons?token=52ee6ca5-8639-422b-bafe-470013c11176&$filter=profileType%20eq%202');

$cliente = json_decode($json);

For more information, read:

Difference between single and double quotes in PHP

  • 1

    Wallace gave it right, Thank you very much! and I tended to it

  • @Jeanprado ok. I realize you’re new to the community. If the question solved the problem, consider voting/tagging as useful the same :p

  • 2

    @Jeanprado [...] Using the on the left side of the response.

  • I didn’t add to the answer, but it’s for these and others that I prefer to use sprintf to leave the string with "slots" and then set the values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.