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":"[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); }});
8
  • stackoverflow.com/a/6849861/3859027 Commented May 3, 2015 at 13:38
  • @Ghost this json url doesn't support callback, can you make me a example similar to my php json code? Commented May 3, 2015 at 13:42
  • then just make a php wrapper which gets it, then call you PHP url instead Commented May 3, 2015 at 13:46
  • @Ghost can you give me a example? I don't get it, as far as i can see you understand well ajax, can you please make me a working example on jsfiddle? Thanks Commented May 3, 2015 at 13:50
  • Can you var_dump the $json and $json_a variables and post them for review? Commented May 3, 2015 at 13:56

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

16 Comments

@Ilie sure no prob, glad this helped
@Ilie if you used $.getJSON then it wouldn't work of course, that sends a GET request, i'm my example, i used POST
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
@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
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?
|
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":"[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"}';

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

3 Comments

actually, the OP wants to process this JSON in javascript
and the issue isn't in the JS in their example
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
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);
});

6 Comments

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
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
can you make me a example how can i make this ? I'm really new in ajax/javascript
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)
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 &amp;&amp; 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?
|

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.