1

I have an API that is available only in javascript (no PHP). It fetches some data of which I made a JSON string. What I need to know is how do I read this string in other page?

I tried using following :

$json = file_get_contents($url);

but of course the string I get is not JSON but is actually the javascript code that will generate json in original page. Any suggestions ? Thank you.

P.S. I also tried set cookie + redirect. Though that worked, I'd like to know a better solution.

Here is the javascript code

 function searchComplete() {
 if (imageSearch.results && imageSearch.results.length > 0)
 {
 var contentDiv = document.getElementById('content');
 contentDiv.innerHTML = '';
 var results = imageSearch.results;
 var data = "{['data' : [\n";
 for (var i = 0; i < results.length; i++) {
 //var result = results[i];
 var result = results[i];
 data += "{\n";
 data += "'url' : '"+result.url+"'";
 data += "\n},\n";
 }
 data += "]]}";
 setCookie("datajson", data, 1); // This is how i set the cookie and redirected
 window.location = "JSPHP.php?data="+data;
 document.getElementById('body').innerHTML = data;
 }
 }
asked May 16, 2011 at 19:04
1
  • Can you show what the Javascript looks like? Commented May 16, 2011 at 19:05

3 Answers 3

2

PHP have great module for it:

json_encode
json_decode
answered May 16, 2011 at 19:06
1
  • mihsathe said: the string I get is not JSON but is actually the javascript code that will generate json in original page. How do you want to use json_decode() for parsing JavaScript code? Commented May 16, 2011 at 19:10
1

Ok, you said it is not JSON. Thus json_decode() function is useless here without additional things. If this happens on browser side, you should probably pass the data to the server using AJAX.

Alternatively you can take a look how this API works (maybe it connects with some server using JSONs to exchange data?) and do the same in PHP.

EDIT:

You do not need to create a string of JSON on JavaScript side. JSON is itself object notation in JavaScript.

If you need to pass the data, just do something similar to this:

var data = [];
for (var i=0; i<results.length; i++){
 data[] = {
 'url': result.url
 };
}

Then you only need to pass this data to the server. You can use .get() function (if you need to pass it using GET) from jQuery like that:

jQuery.get('http://example.com/', data, function(){
 // something to do when successful
});

It is pretty simple and the basic is: create data correctly and pass it to the server using AJAX call.

answered May 16, 2011 at 19:08
0
0

Make yourself a web page that executes the javascript and saves the JSON result. Then post the JSON data to a PHP script (via XmlHttpRequest or regular form POST). Now your PHP code has access to the JSON data, and you can use json_decode to access it.

answered May 16, 2011 at 19:09
2
  • I tried same using a cookie (code added). Problem is when I want to access my json from other (third) page from maybe other web site. The problem is which page do I call. If I call the first, its useless. But if I call second, there's no cookie with it. Commented May 16, 2011 at 19:17
  • Though using database is an option, that will be a shame .. lol Commented May 16, 2011 at 19:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.