PHP 中的命令行 cURL 参数

command line cURL parameters in PHP

我正在开发一个必须连接到 REST API 的 PHP 脚本。 API的提供者建议使用cURL。他们给了我一个如何在命令行中使用它的例子:

curl -D- -u "user:password" -X GET -H "Content-Type: application/json" http://example.com/api/searchFunction?jql=assignee=user1 

PHP脚本如下:

<?php

$defaults = array(
CURLOPT_HEADER => true,
CURLOPT_URL => 'http://example.com/api/searchFunction?jql=assignee=user1', 
CURLOPT_USERPWD => "user:password",
CURLOPT_HTTPAUTH => 'CURLAUTH_BASIC'
);

$ch = curl_init();
curl_setopt_array($ch, ($defaults));

echo "cURL output: ".curl_exec($ch);

curl_close($ch);

 ?>

如您所想,命令行版本工作正常,但在 PHP 版本中出现以下错误:

Field 'assignee' does not exist or this field cannot be viewed by anonymous users.

这表明用户登录验证不起作用。但是,用户和密码是正确的。

我正在寻找已经回答的命令行版本和 PHP 版本之间的 cURL 参数等价物的帖子,但找不到 PHP 版本的正确参数。

您还没有完全复制您的 cURL 命令。

首先,您从未设置 Content-Type: application/json header 选项。您需要使用 CURLOPT_HTTPHEADER 选项进行设置。

其次,命令行 cURL 和 PHP 的 cURL 使用不同的 User-Agent 值。

考虑启用命令行 cURL 的详细选项,这样您就可以看到它发送的所有信息,然后复制它 PHP。