5

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":"alex.costantin@mail.ru","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) "alex.costantin@mail.ru" ["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); }});
Ilie
  • 121
  • 1
  • 1
  • 6

3 Answers3

1

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

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • @Ilie sure no prob, glad this helped – Kevin May 03 '15 at 14:15
  • @Ilie if you used `$.getJSON` then it wouldn't work of course, that sends a `GET` request, i'm my example, i used POST – Kevin May 03 '15 at 15:18
  • i haven't used $.getJSON, i need to use it beacuse if not it get's the data from server side, i need from client side, like this one is made take a look [link](http://www.movietend.com/player-moby.php?oid=97788243&id=163494721&hash=877e091877cd7a11) – Ilie May 03 '15 at 15:22
  • @Ilie haven't you tried out the demo, should be faily straight forward, the PHP get the external JSON, then your clientside (JS) calls your PHP instead – Kevin May 03 '15 at 15:26
  • i did like that but i need to request the content from client side with ajax/javascript than make your code work out, i just need to pull the data from url by client side, can you change your code to work like that, instead of file_get_contents server side to $.getJSON client side? – Ilie May 03 '15 at 15:30
  • @Ilie just use `echo file_get_contents($_GET['oid'])` – Kevin May 03 '15 at 15:50
  • i've explained myself badly, so, `echo file_get_contents($_GET['oid'])` is not a option because i will get content from my server's side, the [Json Video Url](http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json) gives diferent data to every client, and that's why i need to retrieve this data from client side with `$.getJSON` i hope you understood my intentions – Ilie May 03 '15 at 15:53
  • Hi @Ghost i've made this `$.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); }});` but doesn't works, what's wrong with the code? – Ilie May 03 '15 at 17:54
  • @Ilie yes it won't work, its cross origin policy, you can't do that. you need to use PHP to call the external JSON, then use your own JS to call your PHP that calls the external JSON if you can't use JSONP – Kevin May 04 '15 at 01:15
  • Hi @Ghost how can i call the external json from client side? – Ilie May 04 '15 at 01:50
  • @Ilie you can't since you said that this external JSON doesn't support callback. that server needs to support it first – Kevin May 04 '15 at 01:51
  • but i wonder how http://en.savefrom.net/ does because they use java and you can download videos from mail.ru there must be a trick to get the json from client side – Ilie May 04 '15 at 01:56
  • @Ilie i've already answered that, the trick is use your PHP to call the JSON outside your domain, and then use the JS to call your PHP that called the external JSON. i know its not a one way route, but it works – Kevin May 04 '15 at 01:58
  • what do you mean by calling the json outside my domain? can you make me a example please? – Ilie May 04 '15 at 02:00
  • @Ilie its already my answer, and the answer of alexis below, it actually shows the same thing. use your PHP to call the external JSON, then call that PHP using your JS, it seems we're not getting through you – Kevin May 04 '15 at 02:02
  • you don't understand what i mean, look i've made the json to be called from ouside my domain, but my domain still makes the file_get_contents meaning that it will get data based on my domain ip request, so this means that the `+videos[0].url+` will not work to the client [Look at this raw](http://www.movietend.com/datas/raw.php?oid=http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json) and take the .mp4 link and you'll see that is not working! i need to request the json data from client side directly! – Ilie May 04 '15 at 06:30
1

I think its just the way you are accessing the variables, you need to use quotes around your keys

<?php
   $json = '{"version":3,"service":"mail","provider":"ugc","author":{"email":"alex.costantin@mail.ru","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"}';

  $json_a = json_decode($json,true);

  var_dump($json_a["meta"]["poster"]); 
  //string(56) "http://videoapi.my.mail.ru/file/sc03/2500725436577747223"
?>

to set a js variable with your php you can do this:

<script>
    var poster = '<?php echo $json_a["meta"]["poster"]; ?>';
</script>

to set a js object with your php you can do this:

<script>
    var jsonString = '<? phpfile_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json'); ?>';
    var jsonObj = JSON.parse(jsonString);
</script>
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • actually, the OP wants to process this JSON in javascript – Kevin May 03 '15 at 14:20
  • and the issue isn't in the JS in their example – Seth McClaine May 03 '15 at 14:21
  • yes the quotations need to be there but the OP said that: _but i have a hard time to convert this php json into a **javascript function**_ of course your answer would work on the PHP side, but the OP wants the values processed inside JS – Kevin May 03 '15 at 14:23
1

You can use the Simple JSON for PHP library to forge your complex JSON and merge multiple json together without decoding them.

<?php

  include('../includes/json.php');

  // $json = new json(); // Pure JSON
  $json = new json('callback', 'myCallback'); // JSON with Callback

  $jsonOnly = file_get_contents('http://videoapi.my.mail.ru/videos/mail/alex.costantin/_myvideo/4375.json');

  $json->add('status', '200');

  if(connected){
    $json->add("alex.constantin", $jsonOnly, false);
    $json->add("authorized", true);
    // $json->add("authorized"); can also be used
  }
  else 
    $json->add("authorized", false);

  $json->send();
?>

In your HTML you can mainly call it via 2 ways :

Legacy JS & DOM elements.

The callback must be "integrated" with something like : mycallback({ ... });

function load_script(url) {
  var s = document.createElement('script'); 
  s.src = url;
  document.body.appendChild(s);
}

function load_scripts() {
  load_script('myPhpPage');
}

window.onload=load_scripts;

Ajax via Legacy JS or via JQuery

The callback should be like : {} and is called so :

$.getJSON('http://example.com/MyPHP.php',
data,
function(json) {
  alert(json);
});
Alexis Paques
  • 1,885
  • 15
  • 29
  • The content is different for every client, based on ip address, the @Ghost answer was good but i need to get it via client side with `$.getJSON` – Ilie May 03 '15 at 15:25
  • Then, as I just said : use `$.getJSON`, `$.ajax`, `$.post`, `$.get` to send data (could be a unique token) and retrieve the response. In your PHP, you just have to add some conditions, like verifying the token sent and forging the JSON only if the user is authorized. You can add any object, string, array, number, bool or JSONString via the `Simple JSON for PHP` library – Alexis Paques May 03 '15 at 15:54
  • can you make me a example how can i make this ? I'm really new in ajax/javascript – Ilie May 03 '15 at 16:01
  • This is not in the Javascript where we have to work, but in the PHP part. You CANNOT decide if you are connected in Javascript because the client could add some malicious data. JS can be modified by the client. First : Create your PHP script that make you able to check if you are connected & sending a token (stocked in the SESSION var & the COOKIE) – Alexis Paques May 03 '15 at 16:03
  • i've made this `$.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); }});` but doesn't works, what's wrong with the code? – Ilie May 03 '15 at 17:55
  • I say you again, FIRST make your PHP. Verify by loading the page that the information you get is correct! – Alexis Paques May 03 '15 at 18:55