0

I am using an api which is giving me a very strange json format.. i'm posting its data which i am getting thru it..

info = { "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "f53762dab34022e9d851ab71e0bf166f" };

I'm trying to print this data in php but i'm not able to do that..nothing is showing on my webpage,.... My code are..

First i tried,

<?php
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info=file_get_contents($url);
$info=json_decode($info,true);
echo $info;
?>

My second attempt was,

<?php
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info=file_get_contents($url);
$info=json_decode($info,true);
$info->h;
?>

My last attempt was,

<?php
$url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
$info=file_get_contents($url);
$info=json_decode($info,true);
$info['h'];
?>

Nothing is happening..

please somebody help me

API Url converted...

5
  • $url="http://www.website-name.com/itemInfo/?video_id=IN7o2Iy89WQ&ac=www"; IS this sending a JSON? Are you sure? Commented Sep 26, 2013 at 18:13
  • Can you var_dump($info); after your second and third line and show us the output? Commented Sep 26, 2013 at 18:13
  • @SasankaPanguluri plaese check now.. Commented Sep 26, 2013 at 18:16
  • @TheWolf ...my webpage is showing NULL if i use var_dump($info); Commented Sep 26, 2013 at 18:17
  • 1
    Similar question: stackoverflow.com/questions/17126303/… Commented Sep 26, 2013 at 19:09

4 Answers 4

2

The page is sending "info = { "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "058ce93db26fce4a9f1cb41ae2e7c1bb" };"

You cannot use json_decode on this because the info = and the ; at the end are not json. You have to strip the info = and the ;.

   $url="http://www.website-name.com/a/itemInfo/?video_id=IN7o2Iy89WQ&ac=www";
   $info = file_get_contents($url);
   $info = trim($info, "info = ");
   $info = rtrim($info, ";");
   $json = json_decode($info, true);
   echo $json['status'];
Sign up to request clarification or add additional context in comments.

7 Comments

getting error...Fatal error: Cannot use object of type stdClass as array in /home/lpmqcrax/public_html/download/info.php on line 8
@Kapil Sorry, missing the second argument to json_decode. It works now :-)
but it is not working properly...I want to print $json['h'] value which is f53762dab34022e9d851ab71e0bf166f but it is giving me 0cedafe839fc2337f52355ba959576bb which is unfair...do you why is that happening ?
it is giving all the information correct except the "h" value..for which i'm doing all of this..any way to get the right value.
@Kapil To get the right value, you have to fix the server code.
|
1

The data I got from the URL in your example is

info = { "title" : "Asian Dad: B Again!? (you die)", "image" : "http://i.ytimg.com/vi/IN7o2Iy89WQ/default.jpg", "length" : "2", "status" : "serving", "progress_speed" : "", "progress" : "", "ads" : "", "pf" : "", "h" : "5cddd4d1667f24aa9a0f5a6cc21e24e3" };

That's an executable JavaScript snippet, not actually JSON. The reason your php is failing is due to the 'info =' part... json_encode returns null on decoding failure.

While this is an assignment of a variable to a JavaScript object, that would work as JSON too if you removed the 'info =' and semicolon. Assuming the responses are predictable, you could do this with php str_replace, but finding an API that returns JSON for your source would be a more reliable and clean solution.

Comments

0

If you're getting NULL when you var_dump($info) on the second line as you mentioned in comments, then file_get_contents() doesn't retrieve the data.

It's most likely because allow_url_fopen is set to false in PHP.ini

1 Comment

getting error Warning: Invalid argument supplied for foreach() in /home/lpmqcrax/public_html/download/info.php on line 4
0

From the json_decode() documentation:

Takes a JSON encoded string and converts it into a PHP variable.

If 2nd parameter is set to true it returns an array !

SO your first and second attempt is wrong.! It should work third attempt. Just check if you retrieve the data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.