0

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?

user982853
  • 2,470
  • 14
  • 55
  • 82
  • 4
    That's not JSON, it's Javascript source. You'll have to use string manipulation to cut off everything before the first bracket. (And possibly the trailing semi.) – Alex Howansky Sep 12 '19 at 13:36
  • 2
    It appears this endpoint is intended to be embedded in a ` – Alex Howansky Sep 12 '19 at 13:46

1 Answers1

0

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.

JensV
  • 3,997
  • 2
  • 19
  • 43