【问题标题】:cURL works great in terminal but dies with 500 error in scriptcURL 在终端中运行良好,但在脚本中出现 500 错误而死
【发布时间】:2017-02-03 06:12:14
【问题描述】:

我遇到了与此处描述的问题类似的问题,但建议的解决方案对我不起作用:

PHP CURL GET request returns 500 where bash curl succeeds

建议的解决方案是:

“事实证明,我正在访问的 API 需要一个用户代理来处理所有请求,但没有提供任何信息来表明这一点。”

这对我不起作用。

如果我打开终端窗口并执行以下操作:

curl "http://needi.local:8080/search/uber" -X GET --header 'Accept: application/json' --header 'x-api-key: 2gt7Pt2LU194KKcNnc'

我得到了我期望的所有结果。它工作得很好。

如果我创建这个 PHP 文件:

  <?php
  $command = "curl 'http://needi.local:8080/search/uber' -X GET --header 'Accept: application/json' --header 'x-api-key: 2gt7Pt2LU194KKcNnc'
  echo passthru($command); 

这让我明白了:

  php -f tests/functional/curl_external_api_tests.php 
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                   Dload  Upload   Total   Spent    Left  Speed
  100   109  100   109    0     0   2893      0 --:--:-- --:--:-- --:--:--  2945

  {
    "statusCode": 500,
    "error": "Internal Server Error",
    "message": "An internal server error occurred"
  }

添加用户代理并不能解决问题:

  $command = "curl 'http://needi.local:8080/search/uber' -X GET --header 'Accept: application/json' --header 'x-api-key: 2gt7Pt2LU194KKcNnc' --header 'User-Agent: PHP-test/1.0'

还有什么问题?

[更新:]

如果有人认为这是关于 PHP 的问题,我很抱歉。我应该改写为“相对于我在终端中所做的事情,脚本中可能有什么不同?”我也尝试使用 bash 并且我得到了同样的错误。但是 cURL 在终端中有效。那么当我离开终端转而使用 bash 或 PHP 或其他任何东西时会发生什么变化?

【问题讨论】:

  • 你为什么不使用php curl library
  • 您没有直接从服务器收到 500 服务器错误。您将收到预期的 JSON 响应,其中恰好包含您使用 API 本身的错误。此外,使用passthrough 加上curl 从PHP 调用HTTP API 几乎总是一个非常非常糟糕的主意。看看@RohanKhude 链接到的答案。
  • 如果有人认为这是关于 PHP 的问题,我很抱歉。我应该改写为“相对于我在终端中所做的事情,脚本中可能有什么不同?”我也尝试使用 bash 并且我得到了同样的错误。但是 cURL 在终端中有效。那么当我离开终端转而使用 bash 或 PHP 或其他任何东西时会发生什么变化?

标签: php curl terminal


【解决方案1】:

我怀疑你对 curl 的论点被截断了,所以 curl 永远不会通过标题。请尝试使用escapeshellarg 看看是否有帮助。

【讨论】:

    【解决方案2】:

    最后,我在 API 应用程序中发现了一个错误。因此,对于遇到此问题的任何其他人,我想强调我的 PHP 运行良好。我确实做到了这一点:

    <?php
      $command = "curl 'http://needi.local:8080/search/uber' -X GET --header 'Accept: application/json' --header 'x-api-key: 2gt7Pt2LU194KKcNnc'
      echo passthru($command); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多