Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting php json to javascript getJSON

Hello stackoverflow community, i'm apologising for my ignorance in javascript/ajax but i have a hard time to convert this php json into a javascript function

$json =    file_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json');
$json_a = json_decode($json,true);
$url = $json_a[videos][0][url];
$img = $json_a[meta][poster];
echo $url;
echo $img;

Thanks in advance for any help given

Var_dump Json

string(984) "{"version":3,"service":"mail","provider":"ugc","author":{"email":"[email protected]","name":"alex.costantin","profile":"http://my.mail.ru/mail/alex.costantin"},"meta":{"title":"avg","externalId":"mail/alex.costantin/_myvideo/4375","itemId":4375,"accId":54048083,"poster":"http://videoapi.my.mail.ru/file/sc03/2500725436577747223","duration":7955,"url":"http://my.mail.ru/mail/alex.costantin/video/_myvideo/4375.html","timestamp":1430140403,"viewsCount":13345},"videos":[{"key":"360p","url":"http://cdn28.my.mail.ru/v/54048083.mp4?sign=dab566053f09db40a63a263f17190aeeb09f1d8d&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-v.mp4&p=f&expire_at=1430773200&touch=1430140403","seekSchema":3},{"key":"720p","url":"http://cdn28.my.mail.ru/hv/54048083.mp4?sign=e9ea54e857ca590b171636efae1b80ccdf0bb5bf&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-hv.mp4&p=f&expire_at=1430773200&touch=1430140403","seekSchema":3}],"encoding":true,"flags":16387,"spAccess":3,"region":"200"}" 

Var_dump Json_a

array(10) { ["version"]=> int(3) ["service"]=> string(4) "mail" ["provider"]=> string(3) "ugc" ["author"]=> array(3) { ["email"]=> string(22) "[email protected]" ["name"]=> string(14) "alex.costantin" ["profile"]=> string(37) "http://my.mail.ru/mail/alex.costantin" } ["meta"]=> array(9) { ["title"]=> string(3) "avg" ["externalId"]=> string(33) "mail/alex.costantin/_myvideo/4375" ["itemId"]=> int(4375) ["accId"]=> int(54048083) ["poster"]=> string(56) "http://videoapi.my.mail.ru/file/sc03/2500725436577747223" ["duration"]=> int(7955) ["url"]=> string(62) "http://my.mail.ru/mail/alex.costantin/video/_myvideo/4375.html" ["timestamp"]=> int(1430140403) ["viewsCount"]=> int(13345) } ["videos"]=> array(2) { [0]=> array(3) { ["key"]=> string(4) "360p" ["url"]=> string(185) "http://cdn28.my.mail.ru/v/54048083.mp4?sign=dab566053f09db40a63a263f17190aeeb09f1d8d&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-v.mp4&p=f&expire_at=1430773200&touch=1430140403" ["seekSchema"]=> int(3) } [1]=> array(3) { ["key"]=> string(4) "720p" ["url"]=> string(187) "http://cdn28.my.mail.ru/hv/54048083.mp4?sign=e9ea54e857ca590b171636efae1b80ccdf0bb5bf&slave[]=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F54048083-hv.mp4&p=f&expire_at=1430773200&touch=1430140403" ["seekSchema"]=> int(3) } } ["encoding"]=> bool(true) ["flags"]=> int(16387) ["spAccess"]=> int(3) ["region"]=> string(3) "200" } 

So far i've made this but no success, what's wrong with the code?

$.ajax({ type: "GET", url: "http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json", async: false, beforeSend: function(x) { if(x && x.overrideMimeType) { x.overrideMimeType("application/j-son;charset=UTF-8"); } }, dataType: "json", success: function(data){ alert(data.meta.poster); }});
like image 931
Ilie Avatar asked Oct 20 '22 12:10

Ilie


1 Answers

If you can't get it thru JSONP, you could just create that PHP wrapper that handles the request, then call that PHP url of yours to get it.

Here's a rough example on the same page (of course, it would be much better if you separate the PHP file).

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['json_call'])) {
    echo file_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json');
    exit;
}

?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
$.ajax({
    url: document.URL,
    dataType: 'JSON',
    type: 'POST',
    data: {json_call : true},
    success: function(response) {
        alert(response.version);
        alert(response.videos[0].key);
    }
});
</script>

Sample Output

like image 150
Kevin Avatar answered Oct 27 '22 09:10

Kevin