yes sure yes sure - 3 months ago 8
JSON Question

Invalid JSON decoding in PHP

I have wrote a script to login into another website in PHP

When I log in, the page returns a JSON response where inside there is an URL

In that url there is a session parameter that I want to store in a PHP variable

the response looks like this:

{
status:{
err_code:0,
err_text:""
},
url:"https://example.com/foo/bar?parameter=1&ID=54SomeCasualLettersAndNumbersWithAT@&otherparam=blah2"
}


I have tried to parse the JSON but the format is invalid and now I don't know what to do..

Is it a good idea to count the characters of the response and get the specify ones that I need?
They are form the 112nd to the 123rd

Jan Jan
Answer

You could go for:

<?php

$string = <<<DATA
{
    status:{
        err_code:0,
        err_text:""
    },
    url:"https://example.com/foo/bar?parameter=1&ID=54SomeCasualLettersAndNumbersWithAT@&otherparam=blah2"
}
DATA;

$regex = '~url:"([^"]+)"~';
preg_match($regex, $string, $match);
print_r($match);
?>

The url will be in $match[1].

Comments