duanfan5012
duanfan5012
2019-07-22 08:32

没有得到预期的PHP cURL响应

已采纳

I have the following PHP code:

<?php
$data = array("client_id" => "sipgate-app-web", "grant_type" => "password", "username" => "my_username", "password" => "my_password");
$data_string = json_encode($data);

$ch = curl_init('https://api.sipgate.com/login/sipgate-apps/protocol/openid-connect/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
));

$result = curl_exec($ch);

echo $result;
?>

Unfortunately, I'm not getting the expected response. The response I'm receiving is:

{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}

When using an online cURL tool like https://onlinecurl.com with the same data (URL, header, data) as in my cURL PHP code, I'm getting the right response. This means, there's something wrong with my PHP code. I'm not getting any error in the PHP error log.

The manual says I have to use the following cURL code:

 curl \
--request POST \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Accept: application/json' \
--data-urlencode "client_id=sipgate-app-web" \
--data-urlencode "grant_type=password" \
--data-urlencode "username=my_username" \
--data-urlencode "password=my_password" \
https://api.sipgate.com/login/sipgate-apps/protocol/openid-connect/token

Since I'm new to cURL, after googling a lot, I have no idea what I'm doing wrong.

Can anybody help me?

EDIT: You can test my PHP code above as it is. You should get the following response, if the code is working:

{"error":"invalid_grant","error_description":"Invalid user credentials"}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongya1228 dongya1228 2年前

    As per the manual, your request needs to have the Content-Type of application/x-www-form-urlencoded which looks like this:

    key1=value1&key2=value2
    

    Thus you need to convert your array into such a string either manually or with http_build_query, like so:

    $data_string = http_build_query( $data );
    
    点赞 评论 复制链接分享
  • dongzhao3040 dongzhao3040 2年前

    I am consuming one of the API using following PHP CURL, Give this a try and pass your credentials i.e username and password in "your username" and "your password".

    <?php
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://api.sipgate.com/login/sipgate-apps/protocol/openid- 
    connect/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS =>"{
    \client_id:\"sipgate-app- 
    web\",
    \tgrant_type\"password\",
    \tusername:\"your 
    username\",
    \tpassword:\"your password\"
    }",
    CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Content-Type: application/json"
    ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
    echo "cURL Error #:" . $err;
    } else {
    echo $response;
    } ?>
    
    点赞 评论 复制链接分享

相关推荐