Warning: Trying to access array offset on value of type null in index.php on line 21

204 views Asked by At

Below is my PHP code and I am facing the issue of

Warning: Trying to access array offset on the value of type null in index.php on line 21

Code:

$curl = curl_init();

$url="url";

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array(
        "Accept: */*",
        "Accept-Encoding: gzip, deflate, br",
        "Accept-Language: en-US,en;q=0.9",
        "Cookie: num=8888888; stb_lang=en; timezone=GMT",
        " Host: url.com", 
        "User-Agent: Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) MAG200 stbapp ver: 2 rev: 250 Safari/533.3",
        "Authorization: Bearer D0CF5357F55F39F7D3A1B907D13C7B16",
        ),
));

$response = curl_exec($curl);

curl_close($curl);

$zx = json_decode($response, true);

$xxc= $zx["js"]["cmd"];

$regex = '/\b(http?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i';

preg_match_all($regex, $xxc, $matches);

$urls = $matches[0];

// go over all links

foreach($urls as $url) {
   header("Location: $url");
} 
?>

Before it was working but suddenly i don't know what happened no response now and getting issue.

Can anyone please help me by solving the issue?

2

There are 2 answers

0
Daniele Continenza On BEST ANSWER

Just remove this line

Accept-Encoding: gzip, deflate, br

It causes the response to be coded with gzip, deflate or brotli (br), but you have to decode the response itself.
If you prefer to NOT remove the compression, then modify like this

....
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => 'gzip',
CURLOPT_HTTPHEADER => array (
....

Last option is to leave all as you write and simply modify a little piece of code to have this:

...
curl_close( $curl );
// This tests if the $response is a compressed gzip
$is_zip = strcmp( substr( $response, 0, 2 ), "\x1f\x8b" );
if ($is_zip === 0){
    $response = gzdecode( $response );
}
$zx = json_decode( $response, true );

Maybe with other compression alghoritms this doesn't work fine, so I suggest you to use the SECOND solution I propose.

"Why suddenly stop working?"
Maybe the host (bluesky.panel.tm) has changed the Api (or the server) to compress the answers?

0
aynber On

You're passing "Accept-Encoding: gzip, deflate, br" into your headers, which means the server is sending an encoded response. $response is not a json, so json_decode returns null. Add CURLOPT_ENCODING => 'gzip', to your curl_setopt_array, and curl will be able to decode the response into a proper json string.