0

I have got one function which have code like this

$response = $this->request_validation($cookies, $fields);
$response_errors = json_decode($response, true)['errors'];

Its giving me warning called

Notice: Undefined index: errors in

I am not getting idea how I can handle this warning.

private function request_validation($cookies, $fields){

        $headers = array();
        $headers[] = 'Origin: https://login.yahoo.com';
        $headers[] = 'X-Requested-With: XMLHttpRequest';
        $headers[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36';
        $headers[] = 'content-type: application/x-www-form-urlencoded; charset=UTF-8';
        $headers[] = 'Accept: */*';
        $headers[] = 'Referer: https://login.yahoo.com/account/create?specId=yidReg&lang=en-US&src=&done=https%3A%2F%2Fwww.yahoo.com&display=login';
        $headers[] = 'Accept-Encoding: gzip, deflate, br';
        $headers[] = 'Accept-Language: en-US,en;q=0.8,ar;q=0.6';
      
        $cookies_str = implode(' ', $cookies);
        $headers[] = 'Cookie: '.$cookies_str;


        $postdata = http_build_query($fields);

        
        $url = $this->_yahoo_signup_ajax_url;
                  
        $ch = curl_init($url);
        
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST, true);
        
        curl_setopt( $ch, CURLOPT_ENCODING, "");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        

        
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        

        $result = curl_exec($ch);

        curl_close($ch);
        
      return $result;
    }
}

I am trying to fix it from last one and half hour but not getting idea for fix it. can anyone here please help me for resolve the issue. Thanks!

Manisha
  • 195
  • 1
  • 10
  • do `json_decode($response, true)['errors'] ?? array()`. This notice shows up when you are trying to use the value of an undefined key in a PHP array. The `??` is the null coalescing operator and it returns its first operand if it exists and is not NULL; otherwise it returns its second operand. – Cornel Raiu Oct 09 '22 at 12:56

1 Answers1

1

In this line:

$response_errors = json_decode($response, true)['errors'];

You're assuming that json_decode($response, true) will return an array, and this array will have an element with index 'errors'.

Is not a good idea to assume such thing, you must take care using json_decode because there can be many issues that produces an unexpected result.

Just a simple check could be:

$response = $this->request_validation($cookies, $fields);
$ok = false;
if ($response) {
    $decoded = json_decode($response, true);
    if (is_array($decoded) && array_key_exists('errors', $decoded)) {
        $response_errors = $decoded['errors'];
        $ok = true;
    }
}
//Now you can check $ok to go ahead or not

It will be useful to show an error message when $ok is false.

More to do

An improved check can be done over your curl request. You don't check the result in any way, so here is open a big door to unexpected behaviour.

It would be nice to check curl result, first of all checking if it's false. Well, this basic check is done with the code above, but you can also check HTTP response code.

José Carlos PHP
  • 1,417
  • 1
  • 13
  • 20
  • Thanks for answer but I think array_key_exists('errors', $decode) should be array_key_exists('errors', $decoded), correct ? – Manisha Oct 09 '22 at 15:15