PHP卷曲发错在Content-Type POST请求

问题描述 投票:1回答:2

我试图发送一个POST请求使用的oauth2 AUTH方法VendHQ API。我有正确的代码,CLIENT_ID,client_secret等,因为它在邮差工作得很好,但是当我尝试使用PHP卷曲发送相同的数据,我得到的错误:

错误

'error' => string 'invalid_request' (length=15)
'error_description' => string 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "grant_type" parameter.' (length=179)

这是Requesting Access Token的文件,这是我的代码正在试图获得访问令牌。

PHP代码:

$prefix     = $vend[0]['domain_prefix'];
$request_url = 'https://'.$prefix.'.vendhq.com/api/1.0/token';

$body['code']           = $vend[0]['code'];;
$body['client_id']      = $vend[0]['app_id'];;
$body['client_secret']  = $vend[0]['app_secret'];;
$body['grant_type']     = 'authorization_code';
$body['redirect_uri']   = $vend[0]['redirect_uri'];;

$response = $this->invoke($request_url, 'POST', $body);

调用函数

private function invoke($url, $method, $data = null)
{

    $ch = curl_init($url);

    if($method=='POST'){


        if(isset($data)){
            $data_string = json_encode($data);
        }


        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

        $headers = array();
        $headers[] = "Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
        $headers[] = 'Content-Length: '.strlen($data_string);

        echo '<br>Curl Headers';
        var_dump($headers);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    }//END POST


    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $json = curl_exec($ch);

    $info = curl_getinfo($ch);
    echo '<pre>Curl Info<br>';
    var_dump($info);
    echo '</pre>';

    curl_close($ch);

    $json_output = json_decode($json, true);

    return $json_output;

}//end function

我相信我发送一切正常,但卷曲发送但是从卷曲的信息,我得到这个

'content_type' => string 'application/json; charset=UTF-8' (length=31)

但VendAPI Documentation说发送后的数据作为“应用/的X WWW的形式,进行了urlencoded”。

注意到这些参数应作为发送POST请求的“应用程序/ x WWW的形式进行了urlencoded”编码体

我做错了吗?

php curl
2个回答
1
投票

在这里张贴的问题后解决了这个问题。

问题是,我试图与发布内容application/x-www-form-urlencode类型的数据,但我在JSON格式发送数据。我已经移除调用函数这个线

if(isset($data)){
    $data_string = json_encode($data);
 }

并设置curl_setopt($ch, CURLOPT_POSTFIELDS, $data);和除创建一个$body阵列中,我创建的字符串:

$body   =   'code='.$vend[0]['code'];
$body   .=  '&client_id='.$vend[0]['app_id'];
$body   .=  '&client_secret='.$vend[0]['app_secret'];
$body   .=  '&grant_type=authorization_code';
$body   .=  '&redirect_uri='.$vend[0]['redirect_uri'];

所有工作得很好:)


0
投票

谢谢!下面是使用你的答案后,工作对我来说是完整的例子:

$data = 'grant_type=password';
$data .= '&username='.$username;
$data .= '&password='.$password;
$data .= '&client_id='.$client_id;
$data .= '&client_secret='.$client_secret;

$ch = curl_init();

curl_setopt_array($ch, array(
CURLOPT_URL => $ep_token_issuance,
CURLOPT_POST => true,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
));

$result = curl_exec($ch);

//print_r(curl_getinfo($ch));

curl_close ($ch);

然而,发送的头仍然是内容类型:应用程序/ JSON。

© www.soinside.com 2019 - 2024. All rights reserved.