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...

Kapil
  • 115
  • 3
  • 17

4 Answers4

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'];
Kapil
  • 115
  • 3
  • 17
Nico
  • 6,395
  • 4
  • 25
  • 34
  • getting error...Fatal error: Cannot use object of type stdClass as array in /home/lpmqcrax/public_html/download/info.php on line 8 – Kapil Sep 26 '13 at 18:25
  • @Kapil Sorry, missing the second argument to json_decode. It works now :-) – Nico Sep 26 '13 at 18:26
  • 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 ? – Kapil Sep 26 '13 at 18:47
  • 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 Sep 26 '13 at 18:52
  • @Kapil To get the right value, you have to fix the server code. – Nico Sep 26 '13 at 19:00
  • @kapil the 'h' value is different in all of our sample data! It's probably a hash or access token cookie value... – JAL Sep 26 '13 at 19:04
  • @Nico yes you are right...it is access token..i need this value to generate mp3 file – Kapil Sep 26 '13 at 19:07
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.

JAL
  • 21,295
  • 1
  • 48
  • 66
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

Aristona
  • 8,611
  • 9
  • 54
  • 80
  • getting error Warning: Invalid argument supplied for foreach() in /home/lpmqcrax/public_html/download/info.php on line 4 – Kapil Sep 26 '13 at 18:26
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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arun Kumar M
  • 848
  • 9
  • 14