简体   繁体   中英

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

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] .

That response is malformed JSON and is more of a javascript object. You should be able to use a JSON5 parser to get the data you need. Here's one I found in Packagist:

https://packagist.org/packages/hiroto-k/json5-php

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM