Batch parameter must be a JSON array - php

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

Related

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

how to solve json decode returns null error

My API URL Returned code in browser as shown below. but json_decode($api_url,true); returns null.
i checked json_last_error();, it returns 4(json error syntax).
it worked with json_decode(file_get_contents($api_url),true);
why it isn't work with json_decode. please help
{"dataset":{"id":27153572,"dataset_code":"20MICRONS_A_DEBT","database_code":"DEB","name":"20 Microns Limited,Total Debt","description":"\u003cp\u003e20 Microns Limited(NSE:20MICRONS)-Total Debt(Annual)\u003c/p\u003e","refreshed_at":"2018-09-21T08:04:08.278Z","newest_available_date":"2018-03-31","oldest_available_date":"2005-03-31","column_names":["PERIOD","STANDALONE","CONSOLIDATED"],"frequency":"annual","type":"Time Series","premium":true,"limit":null,"transform":null,"column_index":null,"start_date":"2005-03-31","end_date":"2018-03-31","data":[["2018-03-31",128.56,133.68],["2017-03-31",144.9,151.73],["2016-03-31",155.18,163.41],["2015-03-31",152.8,164.62],["2014-03-31",162.01,176.64],["2013-03-31",148.49,164.73],["2012-03-31",144.67,158.6],["2011-03-31",81.42,120.31],["2010-03-31",84.35,87.35],["2009-03-31",58.62,58.62],["2008-03-31",46.52,null],["2007-03-31",42.46,null],["2006-03-31",40.03,null],["2005-03-31",38.98,null]],"collapse":null,"order":null,"database_id":14992}}
What you are trying to do has no sense. $api_url is just an url so when you try to decode it then it doesn't have a json stucture and it will throw an exeption.
What you should decode is the data that this url returns to you.
So First you should get data from url then use json_decode($api_url,true);.
To get data you can use file_get_contents or curl.
Props to u_mulder who hit the nail on the head but I want to break this down for you a little.
Purpose of file_get_contents():
The file_get_contents() function is an inbuilt function in PHP which is used to read the contents of a file into a string.
Please note that the 'file' can be a file residing on your web server or a remote file (URL), which in essence will give you a web document back.
Purpose of json_decode():
The json_decode() function is an inbuilt function in PHP which is used to decode a JSON string. It converts a JSON encoded string into a PHP variable.
With that in mind you can see that performing json_decode on an invalid JSON string or a URL will render your result as NULL.
json_decode('https://www.website.com')
This is essentially what you are doing. performing the file_get_contents inside your json_decode first converts your URL/File ('https://www.website.com') into a string, that string then having JSON is then converted into an array by json_decode.

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

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

Encode a url into base64 and then decode it

I encode a url having GET parameters using base64 and then decode it back to get the same url back. The string I receive is.
task_type=all&keyword=asdfsd&location=Vasant Kunj&category=17&subcategory=19&price[]=&price[]=100&price[]=300&price[]=500&price[]=above&educations[]=&educations[]=22&educations[]=29&industry[]=&industry[]=1&industry[]=7&experience=6&freshness=1
Now how can I get the original $_GET array back so that I can use it. Or any other way of getting all the parameters so that I can use them. Or any other better way to encode and decode so that original url is not visible to the user.
All help appreciated. Thanx in advance.
As you are encoding the url having GET parameters using base64 you will not be able to get exact GET parameters. But after decoding it you are getting the query string back as you have mentioned, So in this case you can use parse_str(), see doc
Example:
$queryString = "task_type=all&keyword=asdfsd&location=Vasant Kunj&category=17&subcategory=19&price[]=&price[]=100&price[]=300&price[]=500&price[]=above&educations[]=&educations[]=22&educations[]=29&industry[]=&industry[]=1&industry[]=7&experience=6&freshness=1";
parse_str($queryString, $getParamsArray);
var_dump($getParamsArray);
I guess this is what you were looking for...

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