0

I parse a parameter to a php file and try to get json with file_get_contents(). This is my Code:

< ?php
    $url = $_GET['url'];
    $url = urldecode($url);
    $json = file_get_contents($url, true);
    echo($json);
? >

This is the called URL: http://vimeo.com/api/v2/channel/photographyschool/videos.json

This is a part of my result:

[{"id":40573637,"title":"All For Nothing - \"Dead To Me\" & \"Twisted Tongues\""}]

And so on... So everything is escaped. There are even \n in the result.

Since I neet to work afterwards with the json (in js), I need a non escaped version!

Interesting thing is, that my code works for example with this json: http://xkcd.com/847/info.0.json

What is my problem?

3
  • Everything isn't escaped, quotes internal to quoted strings here are correctly escaped. Commented May 3, 2012 at 19:43
  • json_decode() correctly decodes that JSON file for me, no problems. Commented May 3, 2012 at 19:44
  • But how do i get the json echo'd so that i could read the result of the php as json? Commented May 3, 2012 at 19:49

4 Answers 4

1

If you just want to proxy/forward the response then just echo it as it is with the correct Content-Type header:

<?php
    header('Content-Type: application/json');
    $json = file_get_contents('http://vimeo.com/api/v2/channel/photographyschool/videos.json');
    echo $json;
?>

Tho you have to be very wary of the url passed as it could cause XSS!

And as the API is slow/resource hungry you should cache the result or at least save it in a session so its not repeated on each page load.

<?php
$cache = './vimeoCache.json';
$url = 'http://vimeo.com/api/v2/channel/photographyschool/videos.json';

//Set the correct header
header('Content-Type: application/json');

// If a cache file exists, and it is newer than 1 hour, use it
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
    echo file_get_contents($cache);
}else{
    //Grab content and overwrite cache file
    $jsonData = file_get_contents($url);
    file_put_contents($cache,$jsonData);
    echo $jsonData;
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

is there no way just to "echo" the json in the correct json format so that my php is just printing out what vimeo json is delivering?
You mean like this? $json = file_get_contents(...); echo $json;
1

Use this:

echo json_decode($json);

EDIT: FORGET the above. Try adding:

header('Content-Type: text/plain');

above

$url = $_GET['url'];

and see if that helps.

11 Comments

@PhilippSiegfried yes, json_decode will turn the JSON into a PHP array. How are you parsing the JSON in your JS?
I would like to have the php as some sort of proxy to prevent cross-site-scripting problems when i do an ajax call from a different machine!
Use print_r() instead, maybe?
then i get Array ( [0] => stdClass Object ( [id] => 40573637 [title] => All For Nothing - "Dead To Me" & "Twisted Tongues" ... which is no valid json too..
@PhilippSiegfried Try adding header('Content-Type: text/plain'); above $url = $_GET['url']; and if that helps
|
0

You should use json_decode : http://php.net/manual/en/function.json-decode.php

Comments

0

better yet, where you deliver your json use:

json_encode(array(
    "id" => 40573637,
    "title" => 'All For Nothing - "Dead To Me" & "Twisted Tongues"'
));

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.