Curl 正在工作,但是 php CURL 出错

Curl is working however error with php CURL

我们想与我们从 linux 终端使用 curl 的第 3 方 API 通信。卷曲是 - curl -X POST \ \ -H 'Authorization: Bearer ' \ -H 'Content-Type: application/json'

当我们触发这个 curl 时,我们会得到预期的响应。

但是,当我们尝试从 PHP 脚本执行此操作时,我们会收到错误消息 - HTTP 错误 500

PHP 代码片段是 -

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, <URL>);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS,'');
curl_setopt($ch, CURLOPT_VERBOSE,true);

$headers = array();
$headers[] = 'Authorization: Bearer <token>';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

?>

当详细模式打开时收到的响应是 -

*   Trying <IP>...
* Connected to <URL> (<IP>) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server did not agree to a protocol
* Server certificate:
*    subject: OU=Domain Control Validated; CN=* <domain>
*    start date: Dec 17 10:41:01 2017 GMT
*    expire date: Dec 17 10:41:01 2020 GMT
*    subjectAltName: <URL> matched
*    issuer: C=US; ST=Arizona; L=Scottsdale; O=GoDaddy.com, Inc.; OU=http://certs.godaddy.com/repository/; CN=Go Daddy Secure Certificate Authority - G2
*    SSL certificate verify ok.
> POST /app/auth HTTP/1.1
Host: <URL>
Accept: */*
Authorization: Bearer <Token>
Content-Type: application/json
Expect: 100-continue

< HTTP/1.1 500 Request failed.
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Type: text/html;charset=iso-8859-1
< Date: Wed, 08 Jan 2020 04:39:23 GMT
< Content-Length: 252
< Connection: keep-alive
* HTTP error before end of send, stop sending
< 
* Closing connection 0
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 500 Request failed.</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /app/auth. Reason:
<pre>    Request failed.</pre></p>
</body>
</html>

请注意,我在此处发布问题时替换了实际 URL、IP 和令牌。

为了确保 PHP curl 没有问题,我们使用了 curl-to-PHP 代码生成器实用程序来生成代码 (http://incarnate.github.io/curl-to-php/)。

有人可以帮助我,让我知道可能出了什么问题。

为避免 «500 错误»(例如)请务必:

set proper "Referer: " header if needed, with

curl_setopt(CURLOPT_REFERER, 'ref page');

set proper "User-Agent: " header, with

curl_setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');