我无法使用PHP

时间:2018-12-20 22:08:50

标签: php service

我正在尝试使用我在c#中拥有的项目中公开的服务,问题是当从php中使用服务时出现以下错误。 {“错误”:“ unsupported_grant_type”}

代码:

function __construct() {

    # data needs to be POSTed to the Play url as JSON.
    # (some code from http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl)
    $data = array("grant_type" => "password", "username" => "Administrador", "password" => "xxxx");
    $data_string = json_encode($data);

    print_r($data_string);

    $ch = curl_init('https://integraciones.intergrupo.com/rest/oauth/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')
    );
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);

    echo $result;        // Outputs the JSON decoded data
}

1 个答案:

答案 0 :(得分:0)

您正在发送标头'Content-Type: application/x-www-form-urlencoded'
这样,您就不需要json_encode $data,而只需将数组添加到POSTFIELDS:

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

或将标题更改为'Content-Type: application/json'