How to get data in php curl with square brackets query? - php

I try to get data with libcurl in PHP and my query, value in query, contains query brackets
?conditions[prs_id]=7&conditions[date]=[2019-03-26 TO 2019-04-03]&page=1&limit=1
When I send this with Postman I get object, but with curl i get error 400 which says this is bad request.
but with
?conditions[prs_id]=7&conditions[date]=2019-03-26
I get answer and requested objects.
So The problem is with value with brackets [2019-03-26 TO 2019-04-03]
I tried with url encoding, changing brackets to url codes but it won't worked.
How to properly send this with libcurl in PHP? in Postman works

Try using urlencode() function in the query. I am not sure it will work or not but you can give it a try

Use this function to build query params in url http_build_query

Related

Use brackets with wp_remote_get

I need to get data from another website via WP-REST-API, but the response is too large. So I want to trim it down to only the data I need:
wp_remote_get('https://www.example.com/wp-json/wp/v2/custom_post_type/?_fields[]=link&_fields[]=content&_fields[]=featured_media&per_page=100');
When I use brackets ([]) inside the URL I get a fatal error.
Is there another way to limit the response for the wp_remote_get() function?

I can't send a parameter containing curly brackets via get method in php curl

I'm working on application APIs. I'm trying to create a bot to pull data with php. In short, I need to send a parameter under the GET method, this parameter contains a json data, so I cover it with curly brackets, but when I do this, php curl does not work properly and I make a request to a url that I do not want, can you help me please?
Use json_encode() to create the JSON, and urlencode() to encode it properly for use in a URL.
$param = urlencode(json_encode(['need' => 'help']));
curl_setopt($ch, CUROPT_URL, "example.com/api/?params=$param");

Batch parameter must be a JSON array

Im trying to make following BATCH request on Facebook Graph API
I encode this using urlencode
/?batch=[{"method":"POST","relative_url":"23843240845450549","body":"end_time=2019-03-29T19:59:59-0300&lifetime_budget=5328"}]
This is the encoded request
/?batch=%5B%7B%22method%22%3A%22POST%22%2C%22relative_url%22%3A%226118436634612%22%2C%22body%22%3A%22end_time%3D2019-03-29T19%3A59%3A59-0300&lifetime_budget=328921%22%7D%5D
But i get
"Batch parameter must be a JSON array",
I know the error is around the '&', Im unable to find the way to put multiple fields on body request
Have also tried
/?batch=[{"method":"POST","relative_url":"23843240845450549","body":"end_time=2019-03-29T19:59:59-0300&lifetime_budget=5328"}]
and also
/?batch=[{"method":"POST","relative_url":"23843240845450549","body":"end_time=2019-03-29T19:59:59-0300&amplifetime_budget=5328"}]
Finally i found that i had to encode the & into %26
So this is working:
/?batch=%5B%7B%22method%22%3A%22POST%22%2C%22relative_url%22%3A%226118436634612%22%2C%22body%22%3A%22end_time%3D2019-03-29T19%3A59%3A59-0300%26lifetime_budget%3D328921%22%7D%5D

How can I add a URL to a query string?

I have a small problem with my PHP script. I want to be able to have a URL within a query string so it would look like this:
http://example.com/?url=http://google.com/
This works absolutely fine and $_GET['url'] will return http://google.com.
The problem is when the URL in my query string already has query string, for example:
http://example.com/?url=http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011
will return:
http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8
and I want it to return:
http://www.amazon.com/MP3-Music-Download/b/ref=sa_menu_mp3_str?ie=UTF8&node=163856011
I am using PHP for server side.
Could anybody please help?
Update
I am using Codeigniter, so if this is the reason why it isn't working as it should then please let me know.
You need to encode the url passed as query argument:
If you send it from PHP, use urlencode or rawurlencode.
If you send it from JS, use encodeURIComponent.
Use urldecode() to pass query string

Load Json data with php giving an error

I am using this code to try to get json data from many json files using yql to compile them into one json.
My yql query is in this format:
select * from json where
url="http://ebird.org/ws1.1/data/obs/region_spp/recent?rtype=subnational2&r=US&sci=Dendrocygna%20autumnalis&fmt=json"
or url = "http://ebird.org/ws1.1/data/obs/geo_spp/recent?lng=-119.859512&lat=34.410240&sci=Dendrocygna%20autumnalis&dist=49&back=14&maxResults=10000&fmt=json&includeProvisional=true"
and itemPath = "json.json"
I rawurlencode the above to obtain the q= part of the yql query url. Then i get this outputted url. It works fine when copy and paste the echoed url into my browser, but when i try to use file_get_contents on the url I get this:
cbfunc({"error":{"lang":"en-US","diagnostics":{"publiclyCallable":"true"},"description":"Query syntax error(s) [line 1:3675 missing EOF at '_']"}});
So I am asking, why does it not work when I am using it with file_get_contents, but does when I am using the url in my browser. Also, is there a smarter/easier way to use multiple json files together with php? I tried using curl to pull the yql json and got a weird output like:
8<\/howMany>34.4136<\/lat>-119.8756<\/lng>L615794
This method would be fine, but I don't know how to get it into a useable array.
Try to use urlencode() instead of rawurlencode(). The system you're sending it to may not like the encoding.

Categories