php 我需要知道如何调用curl与API关键参数与给定的命令

nzkunb0c  于 2022-12-25  发布在  PHP
关注(0)|答案(2)|浏览(116)

我有一些开发人员提供的curl命令,我需要知道如何在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;

我期待结果,但它的给予
stdClass Object([message] =〉授权标头需要'Credential'参数。授权标头需要'Signature'参数。授权标头需要'SignedHeaders'参数。授权标头需要存在'X-Amz-Date'或'Date'标头。授权= FLUdjGvxuJ 9 liutivB 0 Ll 5LW 2 t2 xmv 31 lvK 2guQi)

wz8daaqr

wz8daaqr1#

你忘记添加x-api-key头到你的php curl_ code.还有你的php代码正在改变AcceptAccept-Language,和Authorization头,你的curl调用证明这是不需要的,并且他们可能是不正确的 Boot (idk),摆脱他们while debugging(并且一旦一切都工作,你可以添加他们回来并且检查他们不打破任何东西..)

7xllpg7q

7xllpg7q2#

按照此方法发送API REQUEST头中的API_key

// Collection object
    $ch = curl_init($url);
    
 $headers = array(
         "APIKEY: PUT_HERE_API_KEY",
         "Content-type: text/xml;charset=\"utf-8\"",
         "Accept: text/xml"
     );
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_TIMEOUT, 60);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlreq);
    $result = curl_exec($ch); // execute
    $result;
   //show response
    curl_close($ch);

相关问题