简体   繁体   English

PHP中的命令行cURL参数

[英]command line cURL parameters in PHP

I'm working on a PHP script that has to connect to an REST API. 我正在研究必须连接到REST API的PHP脚本。 The provider of the API, suggested to use cURL. API的提供者,建议使用cURL。 They gave me an example of how to use it in the command line: 他们给了我一个如何在命令行中使用它的例子:

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

The PHP script is the following: 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);

 ?>

As you can imagine, the command line version works fine, but in the PHP version I got the following error: 可以想象,命令行版本可以正常工作,但是在PHP版本中,出现以下错误:

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

That suggests that the user login validation doesn't works. 这表明用户登录验证无效。 However, the user and password are correct. 但是,用户名和密码正确。

I was looking for already answered posts of cURL parameters equivalents between the command line version and the PHP version but couldn't find the correct parameters for the PHP version. 我正在寻找命令行版本和PHP版本之间的cURL参数等效项的已答复帖子,但找不到适用于PHP版本的正确参数。

You haven't fully replicated your cURL command yet. 您尚未完全复制cURL命令。

For starters, you've never set the Content-Type: application/json header option. 对于初学者,您从未设置Content-Type: application/json标头选项。 You need to set that using the CURLOPT_HTTPHEADER option. 您需要使用CURLOPT_HTTPHEADER选项进行设置。

Secondly, command line cURL and PHP's cURL use different User-Agent values. 其次,命令行cURL和PHP的cURL使用不同的User-Agent值。

Consider enabling the command line cURL's verbose option so you can see all the information it's sending, then replicate it PHP. 考虑启用命令行cURL的verbose选项,以便您可以查看其发送的所有信息,然后将其复制为PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM