繁体   English   中英

使用PHP cURL连接到OpenID显示错误

[英]Connecting to OpenID using PHP cURL Showing Error

我正在使用以下代码从使用OpenID协议的IdentityServer请求令牌:

$curl = curl_init( 'https://remoteserver.com/connect/token' ); 
curl_setopt( $curl, CURLOPT_POST, true );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
$code = $_GET['code'];  // The code from the previous request
$redirect_uri = 'http://mycalldomain.com/test.php';

curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
    'redirect_uri' => $redirect_uri,
    'grant_type' => 'authorization_code'
) );

curl_setopt( $curl, CURLOPT_USERPWD,
    "MYCLIENTID" . ":" . 
    "MYCLIENTSECRET");

$auth = curl_exec( $curl ); 
print '$auth = ';print_r($auth); // to see the error
$secret = json_decode($auth); 
$access_key = $secret->access_token;

正在输出以下错误:

$auth = {"ErrorMessage":"Unsupported Mediatype"}

有人可以指导吗?

您应该通过添加以下内容来提供您正在发布内容的资源接受的Content-Type HTTP标头:

curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

这将使其成为JSON(例如!),并且您的输出数据(CURLOPT_POSTFIELDS)必须与您选择的Content-Type相对应。

目前,根据PHP文档 ,Content-Type为“ multipart / form-data”:

如果value是一个数组,则Content-Type标头将设置为multipart / form-data。

如果要使用内容类型“ application / x-www-form-urlencoded”,则除了将其设置为内容类型之外,还必须以该格式提供CURLOPT_POSTFIELDS。 作为Web开发语言,PHP具有内置函数http_build_query用于以该格式编码数组:

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
    'redirect_uri' => $redirect_uri,
    'grant_type' => 'authorization_code'
)));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM