OptimusCrime
OptimusCrime

Reputation: 14863

Parse JavaScript from remote server using curl

I need to grab a json-string from this page: https://retracted.com

If you view the source, I json-string starts after var mycarousel_itemList =. I need to parse this string as a correct json-array in my php-script.

How can this be done?

EDIT: I've managed to pull this off using explode, but the method is ugly as heck. Is there no build-in function to translate this json-string to a array?

To clarify: I want the string I grab (which is correct json) to be converted into a php-array.

Upvotes: 2

Views: 1553

Answers (2)

Gordon
Gordon

Reputation: 316969

The JSON in the script block is invalid and needs to be massaged a bit before it can be used in PHP's native json_decode function. Assuming you have already extracted the JSON string from the markup (make sure you exclude the semicolon at the end):

$json = <<< JSON
[ { address: 'Arnegårdsveien 32', … } ]
JSON;

var_dump(
    json_decode(
        str_replace(
            array(
                'address:',
                'thumb:',
                'description:',
                'price:',
                'id:',
                'size:',
                'url:',
                '\''
            ),
            array(
                '"address":',
                '"thumb":',
                '"description":',
                '"price":',
                '"id":',
                '"size":',
                '"url":',
                '"'
            ),
            $json
        )
    ,
    true
    )
);

This will then give an array of arrays of the JSON data (demo).

In other words, the properties have to be double quoted and the values need to be in double quotes as well. If you want an array of stdClass objects instead for the "{}" parts, remove the true.

You can do this either with str_replace as shown above or with a regular expression:

preg_match('
    (.+var mycarousel_itemList = ([\[].+);.+function?)smU',
    file_get_contents('http://bolig…'),
    $match
);
$json = preg_replace(
    array('( ([a-z]+)\:)sm', '((\'))'),
    array('"$1":', '"'),
    $match[1]
);
var_dump(json_decode($json, true));

The above code will fetch the URL, extract the JSON, fix it and convert to PHP (demo).

Upvotes: 1

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

Once you have your json data, you can use json_decode (PHP >= 5.2) to convert it into a PHP object or array

Upvotes: 1

Related Questions