gpt4 book ai didi

php - 在 PHP 中带有 header 的 curl 请求

转载 作者:行者123 更新时间:2023-12-04 14:18:21 24 4
gpt4 key购买 nike

我想向 URL 发出 cURL 请求以及以下 header :

'Content-Type: application/json','授权':'基本 XXXXXXXXXX'

我有以下代码:

<?php

$post_url = "https://api.interlinkexpress.com/user/?action=login";

$curl = curl_init($post_url);

$headers = array(
'Content-Type: application/json',
'Authorization': 'Basic XXXXXXXXX'
);
//curl_setopt($curl, CURLOPT_USERPWD, "username":"Password");
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post_data) );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$post_response = curl_exec($curl);

?>

结果显示为:

Client sent a Bad Request

这是 $header 数组下的“授权...”行

感谢任何建议/帮助。

最佳答案

问题出在标题中。它需要修复。授权将像这样的整个字符串:

$headers = array(
'Content-Type: application/json',
'Authorization: Basic XXXXXXXXX'
);

让我给你一个完整的 curl 例子:

 function CurlSendPostRequest($url,$request)
{
$authentication = base64_encode("username:password");

$ch = curl_init($url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
// CURLOPT_ENCODING => "utf-8", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 20, // timeout on connect
CURLOPT_TIMEOUT => 20, // timeout on response
CURLOPT_POST => 1, // i am sending post data
CURLOPT_POSTFIELDS => $request, // this are my post vars
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
CURLOPT_VERBOSE => 1,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $authentication",
"Content-Type: application/json"
)

);

curl_setopt_array($ch,$options);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
//echo $curl_errno;
//echo $curl_error;
curl_close($ch);
return $data;
}

关于php - 在 PHP 中带有 header 的 curl 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28064737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com