PHP中的API curl-在头文件中授权curl

qnakjoqk  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(204)

我使用这个php示例通过API获取有关列表的详细信息,它可以工作。

$url = "http://api.example.com/apiListing/get";

$input = array (
    userId => xxx, // authentication userId
    loginToken => 'xxx', // authentication loginToken
    "id" => "11011947"
);

$data_string = http_build_query($input);

$url = "$url?$data_string";

$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);

curl_close($ch);

echo $result;

字符串
我必须将站点授权更改为站点仅由HEADER中的userId和Token授权
这是来自API规范的示例:

curl --request POST -H "Content-Type=multipart/form-data" -H "SiteAuth:<userId>:<Token>" "https://api.example.com/site/listing?id=<listingId>"


如何在PHP curl中的HEADER中设置登录名?

x7yiwoj4

x7yiwoj41#

这是你的命令示例的等价形式:

<?php
$userId = "<userId>";
$token = "<Token>";
$listingId = "<listingId>";

$url = "https://api.example.com/site/listing?id=" . urlencode($listingId);

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);

$headers = array(
    "Content-Type: multipart/form-data",
    "SiteAuth: $userId:$token"
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);

if ($response === false) {
    echo "Error: " . curl_error($ch);
} else {
    echo "Response: " . $response;
}

curl_close($ch);
?>

字符串

相关问题