Baki Kocak
Baki Kocak

Reputation: 388

Requested Json parse failed from a php file

I am trying to make an application for LG Smart TV. In index.html, trying to get an json from a website.

This is how i call the json located in my website: (note that it is called test.php in LG ide)

 <?php
header("Content-type:application/json; charset=utf-8");
echo get_url("http://website.com/proxy/collection.php?format=json&main_category_id=2");
function get_url($request_url){
$ch=curl_init($request_url);
curl_setopt($ch,CURLOPT_URL, $request_url);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>

and in index.html, the code below trying to parse json using ajax:

$.ajax({ 
type:"get"
,dataType: 'json' 
,url: 'test.php'
,success: function(jsonData){
//things to do

}
});

The other thing is that when i try to run this code in localhost using xampp there is no problem, i mean the code perfectly works but when it comes to compiling in LG IDE I am getting requested JSON parse error. What could be the problem?

Upvotes: 1

Views: 874

Answers (1)

Friedrich
Friedrich

Reputation: 2300

This are the things which are wrong with your code:

  • You are missing a ; in line 2
  • Your function should get the called name, that should look like this: function get_url($url) { ...
  • You have to give a url to the curl_init with curl_init($url)
  • In the line with the return you wrote response, it has to be $response
  • When you are returning a json you should set the header as well with header("Content-type: application/json; charset=utf-8");

I hope thats all. You could post the errors as well to get better support. See here how to show them.

Upvotes: 1

Related Questions