我正在与交谈。我确信问题不是SS,而是我的PHP Curl帖子的格式,就好像我通过终端发送这个请求一样,我得到了一个正确的响应。
终点站卷曲中心如下:
curl "https://api.shutterstock.com/v2/images/licenses?subscription_id=$SUBSCRIPTION_ID" \
--header "Authorization: Bearer $ACCESS_TOKEN" \
--header "Content-Type: application/json" \
-X POST \
--data '{
"images": [
{ "image_id": "137111171" }
]
}因此,我正在处理将其作为PHP卷发送的问题,下面是我所拥有的:
$url = 'https://api.shutterstock.com/v2/images/licenses?subscription_id='.$SUBSCRIPTION_ID;
$params = new Object();
$params = {
'images' : {'image_id' : '137111171'}
};
$headers = [
'Content-Type: application/json',
'Authorization: Bearer '.$ACCESS_TOKEN
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_decode($params));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Butterfly');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
/*$json = json_decode($response, true);
if (json_last_error()) {
echo '<span style="font-weight:bold;color:red;">Error: ' . $response . '</span>';
} else {*/
return $response;响应表单Shutter是"Decode body failure“,这是一个自定义的错误响应。我认为问题在于$params变量以及它是如何格式化的。问题是,这是一个帖子,我怀疑在另一边,党卫军正在具体地解码这一点。正确的curl参数位于上面的bash curl中,如下所示:
--data '{
"images": [
{ "image_id": "137111171" }
]有人对如何正确格式化这个特定的数据值有什么建议吗?这样我就可以把它作为帖子发送了吗?
谢谢
发布于 2018-03-13 11:16:03
您的PHP代码包含无效的语法,而且PHP没有名为Object的类,但是您可能在寻找StdObject,但是即使这样也没有什么意义。另外,您也没有对$SUBSCRIPTION_ID进行urlencoding编码。删除无效的语法部分,使用json_encode,而不是json_decode。
curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( array (
'images' => array (
array (
'image_id' => '137111171'
)
)
), JSON_OBJECT_AS_ARRAY ) );(按注释编辑,api要求适用的数据为数组而不是对象,因此我添加了JSON_OBJECT_AS_ARRAY标志。)
发布于 2018-03-13 10:42:46
我认为你传递了错误的CURLOPT_POSTFIELDS数据。尝试:
$url = 'https://api.shutterstock.com/v2/images/licenses?subscription_id='.$SUBSCRIPTION_ID;
$params = [
'images' => ['image_id' => '137111171']
];
$headers = [
'Content-Type: application/json',
'Authorization: Bearer '.$ACCESS_TOKEN
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_USERAGENT,'Butterfly');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;https://stackoverflow.com/questions/49253844
复制相似问题