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;
                  }
            }
share|improve this question

Can you show what the Javascript looks like? – Pekka May 16 '11 at 19:05
feedback

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
got it. Thanks :) – mihsathe May 16 '11 at 19:41
You are welcome :) – Tadeck May 16 '11 at 20:03
feedback

PHP have great module for it:

json_encode
json_decode
share|improve this answer
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? – Tadeck May 16 '11 at 19:10
@Tadeck you're right. That's not what I want. – mihsathe May 16 '11 at 19:12
feedback

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.

share|improve this answer
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. – mihsathe May 16 '11 at 19:17
Though using database is an option, that will be a shame .. lol – mihsathe May 16 '11 at 19:22
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.