Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request URL containing $ doesn't work well with PHP - cURL #284

Closed
honeyyadav opened this issue Jun 14, 2020 · 3 comments
Closed

Request URL containing $ doesn't work well with PHP - cURL #284

honeyyadav opened this issue Jun 14, 2020 · 3 comments

Comments

@honeyyadav
Copy link

Short Explanation

When I am generating PHP - cURL code from a postman request with URL containing a $ sign followed by a letter or underscore (for example $firstName, $_address, $LastName), then the code generated by postman doesn't work. This is because PHP treats them as variable name (but they are not variable name(s) in this case).

Example

My request URL is : -

> http://example.com/path?$filter=name eq John

The code generated by postman : -

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://example.com/path?$filter=name%20eq%20John",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

The above code will not work as PHP will treat $filter as a variable name. To solve this problem we need to put a \ (backslash) before $filter like this : -

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://example.com/path?\$filter=name%20eq%20John",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

And now it will work flawlessly.

Postman version used: v7.26.0

@erdemkose
Copy link

Instead of escaping $ characters, replacing double quotes(") with single quotes(') would be a simpler and cleaner solution. PHP does not look for variables in single-quoted strings.

@shellstrom
Copy link

I'm not entirely sure this is the exact same thing, but I'm errors from an API when I use the PHP - cURL option and the postfields look like this. I can run this no problem with bash curl export equivalent in Postman, but for some reason this isn't working out of the box with the PHP - cURL option.
However, @honeyyadav 's suggestion worked out for me, by prepending every $ with a \, resulting in \$...

CURLOPT_POSTFIELDS` =>"[\r\n    {\r\n        \"name\": null,\r\n        \"variables\": {\r\n            \"postId\": \"502\",\r\n            \"next\": \"520\",\r\n            \"previous\": null,\r\            \"start\": 50,\r\n            \"end\": null\r\n        },\r\n        \"query\": \"query ($postId: ID!, $next: String, $previous: String, $start: Int, $end: Int) {\\n  posts: article(id: $postId) {...

@webholik
Copy link
Collaborator

Fixed by #352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants