【问题标题】:trying to access array offset on value of type null (php 7.4, Laravel 7.10.3)尝试访问 null 类型值的数组偏移量(php 7.4,Laravel 7.10.3)
【发布时间】:2020-09-10 17:12:25
【问题描述】:

So everything looks like this

因此,我从 API 指南中获取的简单代码 sn-p 中收到了您可以在标题中看到的错误消息。我尝试使用 PHP 7.3 也不会改变我的问题。我对 PHP 和 laravel 很陌生,我认为有一种简单的方法可以解决这个问题,但我找不到。我正在使用 PHP 7.4。和 Laravel 7.10.3。如果有人可以帮助我,我将非常感激。

<?php
if (isset($_GET['query']) && $_GET['query'] != '') {
  $url = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  $query_fields = [
          'autoCorrect' => 'true',
          'pageNumber' => 1,
          'pageSize' => 10,
          'safeSearch' => 'false',
          'q' => $_GET['query']
  ];
  $curl = curl_init($url . '?' . http_build_query($query_fields));
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_HTTPHEADER, [
          'X-RapidAPI-Host: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
          'X-RapidAPI-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  ]);
  $response = json_decode(curl_exec($curl), true);
  curl_close($curl);
  $news = $response['value'];
}
?>

【问题讨论】:

  • 您好 Pody,请提供您收到的回复以及您需要访问哪些字段的屏幕截图。
  • 谢谢,我添加了一张图片来更好地解释它。
  • dd($response) 提供了什么?

标签: php laravel curl


【解决方案1】:

尝试访问 null 类型值的数组偏移量意味着 $response 为 null,但您仍然尝试从中访问 ['value']。

让我们首先将日志添加到您的 curl 中。

// Get request info as an array
$info = curl_getinfo($curl);

$response = curl_exec($curl); // Notice* don't try to decode without sanitizing/checking the response
curl_close($curl);

// Log the actual response from the remote server
Log::info('Curl response: ' . $response); // Are you seeing *value* in the response. If yes, then do json_decode()

让我在下面的 cmets 中发布消息,我会相应地更新答案。

【讨论】:

  • 感谢您的帮助,我收到以下 ViewException curl_exec():提供的资源不是有效的 cURL 句柄资源
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多