我需要知道如何使用给定命令使用api键参数调用curl [英] i need to know how to call curl with api key parameter with given command

查看:75
本文介绍了我需要知道如何使用给定命令使用api键参数调用curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些开发人员提供的curl命令,我需要知道如何在php中调用它.

i have curl command provided by some developer, i need to know how to call it in php.

命令是:

curl -H "x-api-key: xxxxxxxxxxxxxxxxxxxxxx"
"https://api.civicengine.com/office-
holders?address=1060+W+Addison+Chicago+IL"

我已经写了这个

$data['api-key']='xxxxxxxxxxxxxxxxxxxxx';
/*$data['host']='https://api.civicengine.com';*/

$url='https://api.civicengine.com/office-holders?address=1060+W+Addison+Chicago+IL';
$ch = curl_init();

// set basic auth
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

// set header accept
$headers = array('Accept:application/json', 'Accept-Language:en_US',"Authorization: Bearear xxxxxxxxxxxxxxxxxxxx");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// set mode to POST
curl_setopt($ch, CURLOPT_POST, 1);

// add post fields (1)

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Set the url
curl_setopt($ch, CURLOPT_URL, $url);

// Execute
print_r($result = json_decode(curl_exec($ch)));die;

我希望得到结果,但会有所帮助

i expect result but its giving

stdClass对象([message] =>授权标头需要'Credential'参数.授权标头需要'Signature'参数.授权标头需要'SignedHeaders'参数.授权标头需要存在'X-Amz-Date'或日期标题.Authorization = FLUdjGvxuJ9liutivB0Ll5LW2t2xmv31lvK2guQi)

stdClass Object ( [message] => Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=FLUdjGvxuJ9liutivB0Ll5LW2t2xmv31lvK2guQi )

推荐答案

您忘记将 x-api-key 标头添加到您的php curl_代码中.另外您的php代码正在更改 Accept Accept-Language Authorization 标头,您的curl调用证明这是不需要的,并且它们可能不正确引导(idk),在调试时摆脱它们(一旦一切正常,您可以将它们重新添加并检查它们没有损坏任何东西.)

you forgot to add the x-api-key header to your php curl_ code. also your php code is altering the Accept Accept-Language, and Authorization header, your curl invocation proves that's not needed, and they may be incorrect to boot (idk), get rid of them while debugging (and once everything is working, you may add them back and check that they don't break anything..)

这篇关于我需要知道如何使用给定命令使用api键参数调用curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆