php - laravel php curl 请求 Finerworks api
问题描述
我想使用 finerworks api,我的 curl 代码在这里
$data1 = array(
'web_api_key' => '********-****-****-*****-************',
'app_key' => '********-****-****-*****-************',
);
$data2['credentials'] = $data1;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.finerworks.com/v3/test_my_credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data2),
CURLOPT_HTTPHEADER => array(
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
print_r(json_decode($response));
}
当我在这里运行此代码时,下面是我的请求响应
stdClass Object ( [Message] => The request is invalid. [ModelState] => stdClass Object ( [ApiC.Models.authorization_credentials] => Array ( [0] => Missing or unauthorized api credentials ) ) )
请帮我解决这个问题提前谢谢
解决方案
在标头中传递凭据
$data1 = array(
'web_api_key' => '********-****-****-*****-************',
'app_key' => '********-****-****-*****-************',
);
$data2['credentials'] = $data1;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.finerworks.com/v3/test_my_credentials",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data2),
CURLOPT_HTTPHEADER => array(
"accept: */*",
"accept-language: en-US,en;q=0.8",
"content-type: application/json",
"web_api_key: YOUR web_api_key",
"app_key: YOUR app_key",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:".$err;
} else {
print_r(json_decode($response));
}
推荐阅读
- unity3d - 如何使用 Vuforia 为桌面构建 Unity 并使用网络摄像头作为视频输入?
- css - 如何在 CSS 上将我的网站上的帖子标题居中?
- google-cloud-platform - 我在GCP的AI平台上提交训练操作失败
- jquery - 如何找到带双引号的引号?
- c# - 有没有更好的方法来确定两个实体之间的平移距离小于 x 量,然后分配一个 int 值?
- python - 计算多维数组中索引处的元素
- excel - 使用宏将分隔文件导入 Excel;运行时错误“1004”
- c# - 从 Internet Explorer 网页检索 ID/类
- python-3.x - 使用 linerrud 数据集的感知器学习
- r - 在 R 中以交互方式运行多个 Windows 系统命令