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.
Related
I am loading an external JSON file. Which seems to load fine. Im using this script to load it:
$file ="https://creator.zoho.com/api/json/los/view/All_clients?
authtoken=xxx";
$bors = file_get_contents($file);
When i dump the results, I get:
string(505) "var zohoappview55 = {"Borrowers":[{"Full_Name":"Mike Smith","Email":"dadf#gmail.com","Address":"111 S. Street Ct., Aurora, CO, 80012","Position":"Borrower","ID":"1159827000004784102","Mobile":"+13033324675","Application":"Application 1 - 1159827000004784096"},{"Full_Name":"Stacy Smith","Email":"sdfa#gmail.com","Address":"111 S. Street, 80012","Position":"Co-Borrower","ID":"1159827000004784108","Mobile":"+1303558977","Application":"Application 1 - 1159827000004784096"}]};"
Looks like the json has a predefined var zohoappview55 at the begining of the json. Not sure if this is my issue but when i use json_decode it doesn't not decode. If i remove this beginning variable it decodes just fine.
i don't have a way to change this variable or edit the json file as it's a remote file. Does anyone know how to decode it in the native format with the variable at the beginning?
Having a quick look through the API documentation of zoho, it seems it should normally return correct json. It may think that it's a browser requesting the file as a javascript source so you may need to add an Accept header to your request.
This cannot be done with file_get_contents so you will probably need to use curl instead.
Try to perform a normal php curl request with the header Accept: application/json.
See: PHP cURL custom headers for reference.
But as Alex Howansky said in the comment. The API might not be intended for that. In that case you will need to strip the beginning and end of the received document.
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.
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
I've been trying to parse odata's xml and json data with simplexml and json_decode, and it's not working. I'm getting useless data back, when I query for data and place the link in the address bar I can see the data I want in there. When I place the link in a simplexml or json_decode method and do a var_dump(), none of the data is in there.
It's just a bunch of odata links, to get the data. For example,
My original link, domain.com/1.0/DataService/Titles(4563)/?devid={...}.
This data will show the genre, title, and description, but inorder for me to get the Genre or Description, I will need to use this url which is,
domain.com/1.0/DataService/Genre(4563)/?devid={...} or domain.com/1.0/DataService/Description(4563)/?devid={...}.
If using the oData SDK makes it easier to get the data, that's great, but I've read the SDK installation located here, http://odataphp.codeplex.com/. I'm just not sure where to begin with it.
EDIT
Here's some sample code
$url = "http://api.internetvideoarchive.com/1.0/DataService/EntertainmentPrograms()?$expand=MovieCategory,Director,Copyrightholder&$select=MovieCategory/*,Director/*,Copyrightholder/*&developerid=bafd5091-a36d-4103-b435-638dc55d2122&format=atom";
$xml = simplexml_load_file("$url");
var_dump($xml);
You can change atom to json to get the json data.
The problem in your sample code is the double quotes when defining $url. PHP will try to interpolate $expand, $select, etc. as variables. Since they don't exist as variables in your code, they will just be removed.
The result of this is that you lose the $expand in the url, which will make MovieCategory and the other navigation links show up just as URLs to the data instead of including the data inline.
Use single quotes instead:
$url = 'http://api.internetvideoarchive.com/1.0/DataService/EntertainmentPrograms()?$expand=MovieCategory,Director,Copyrightholder&$select=MovieCategory/*,Director/*,Copyrightholder/*&developerid=bafd5091-a36d-4103-b435-638dc55d2122&format=atom';
$xml = simplexml_load_file($url);
var_dump($xml);
I have a json_string in my database.
I echo and parse it to an object in javascript
I do
$.parseJSON('<?php echo $json_string;?>');
I get a json parse error.
What should I be doing?
This is my json_String
{"patches":[[{"diffs":[[1,"\u000a\u000a printhellon() {\u000a\u000a\u000a}d\u000a\u000a\u000a"]],"start1":0,"start2":0,"length1":0,"length2":26}],[{"diffs":[[0,") {\u000a\u000a\u000a}d"],[1,"s"],[0,"\u000a\u000a\u000a"]],"start1":15,"start2":15,"length1":11,"length2":12}],[{"diffs":[[0," {\u000a\u000a\u000a}ds"],[1,"d"],[0,"\u000a\u000a\u000a"]],"start1":16,"start2":16,"length1":11,"length2":12}]],"times":[1314489779299,1314489779408,1314489779581]}
I think JSON parsers don't like line breaks in strings for some reason. Parsing worked for me after removing the \u000a characters.
Edit: just like Brad said, it would be better to include the code directly as an object. Parsing JSON is usually more useful for data obtained using Ajax or something.
From your example, it appears like you're trying to insert PHP code into your javascript. You can't use PHP like that. PHP is server side, while Javascript runs in the browser after the page has been downloaded.
If you must get data from PHP to your javascript, you need to use AJAX. It's actually really easy with JQuery. Check out http://api.jquery.com/jQuery.ajax/