PHP Curl无法发送HTTP HEADER授权密钥

时间:2018-06-06 04:39:32

标签: php curl

PHP Curl无法发送HTTP HEADER授权密钥。

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_POST => 1,
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_HTTPHEADER, array( 'Authorization: Secret <secret>' )
));

$result = curl_exec($curl);
curl_close($curl);
echo $result;

<secret>,我发送秘密。但显示错误是授权标题为空

Curl getinfo是

Array
(
    [url] => http://x.x.x.x:8090/token/create
    [content_type] => application/json
    [http_code] => 400
    [header_size] => 158
    [request_size] => 142
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.672836
    [namelookup_time] => 4.6E-5
    [connect_time] => 1.331548
    [pretransfer_time] => 1.331722
    [size_upload] => 0
    [size_download] => 37
    [speed_download] => 22
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 1.672799
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => x.x.x.x
    [certinfo] => Array
        (
        )

    [primary_port] => 8090
    [local_ip] => x.x.x.x
    [local_port] => xxxx
)

当我使用Postman或Rest Client时。我发送授权秘密。然后响应成功。

你有什么建议吗? 为什么PHP Curl显示错误但Postman或Rest Client成功运行

1 个答案:

答案 0 :(得分:2)

使用此:

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_HTTPHEADER => array(
        "Authorization: Secret <secret>"
      )
));

$result = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

echo $result;