我已经能够使用适用于chrome的Advanced Rest Client Extension将POST查询发送到特定的HTTPS服务器,并且得到状态代码:200-确定,其主体字段与此代码中使用的主体字段相同,但是当我运行以下代码我得到此响应:403-访问被拒绝。
<?php
$postData = array(
'type' => 'credentials',
'id' => 'exampleid',
'secret_key' => 'gsdDe32dKa'
);
// Setup cURL
$ch = curl_init('https://www.mywebsite.com/oauth/token');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
var_dump($response);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData['published'];
?>
我也注意到,当我使用适用于chrome的Advanced Rest Client Extension时,如果将Content-Type设置为application / json,我必须输入一个登录名和一个密码,我不知道这些是什么,因为即使我输入我在代码中拥有的ID和密钥,它会返回401未经授权。因此,我猜我编写的这段代码没有将其强制为内容类型:application / x-www-form-urlencoded,但我不确定。感谢您在此问题上的任何帮助!
您可以这样尝试一下,看看是否有帮助:
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_USERPWD => 'username:password', //Your credentials goes here
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
CURLOPT_POSTFIELDS => http_build_query($postData),
));
我猜该网站希望在secret_key
您已经提供的基础上进行简单的身份验证。
也可以发送Cookie,以防万一,最好存储它并在下一次Curl调用中再次使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句