-7

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?

  • Which is line 21? – Rob Eyre May 03 '23 at 13:03
  • @RobEyre `$xxc= $zx["js"]["cmd"];` – Legend Akshay May 03 '23 at 13:05
  • 1
    Which would indicate that the variable `$zx` is not what you're expecting. What does it show if you do a `var_dump($zx); exit;` just before this line? – Rob Eyre May 03 '23 at 13:06
  • @RobEyre what should i do?? – Legend Akshay May 03 '23 at 13:09
  • @aynber i provided my whole code, it is the original code. – Legend Akshay May 03 '23 at 13:10
  • As above - you'll need to debug the response that you're getting back from your curl request to bluesky.panel.tm. It's not returning the data you're expecting. You'll need to check the response, as described above – Rob Eyre May 03 '23 at 13:11
  • Also, you have an extra space character before your 'Host' header - probably want to remove that – Rob Eyre May 03 '23 at 13:13
  • Never mind, I didn't scroll down enough – aynber May 03 '23 at 13:14
  • When I use your curl code, `$response` is gibberish. This means `json_decode` will not work and returns a null value. – aynber May 03 '23 at 13:15
  • There are three scenarios: 1) your request isn't reaching the server for some reason, 2) the server is refusing your request for some reason, 3) the server's response isn't formatted as JSON. Only by examining the `$response`, `$xv` variables, and perhaps the PHP error log, will you know which scenario it is. (Just seen @aynber's comment above - looks like scenario 3 then) – Rob Eyre May 03 '23 at 13:16
  • Good code indentation and layout ___is for life, not just for Christmas___ and would help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](https://www.php-fig.org/psr/psr-12/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly May 03 '23 at 13:50

2 Answers2

0

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.

aynber
  • 22,380
  • 8
  • 50
  • 63
-1

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?