PHP Curl在post请求中发送了错误的内容类型

PHP Curl在post请求中发送了错误的内容类型,php,curl,Php,Curl,我正在尝试使用oauth2 auth方法向VendHQ Api发送Post请求。我有正确的代码、client_id、client_secret等,因为它在Postman中运行良好,但是当我尝试使用PHP curl发送相同的数据时,我得到了错误: 错误 'error' => string 'invalid_request' (length=15) 'error_description' => string 'The request is missing a required param

我正在尝试使用oauth2 auth方法向VendHQ Api发送Post请求。我有正确的代码、client_id、client_secret等,因为它在Postman中运行良好,但是当我尝试使用PHP curl发送相同的数据时,我得到了错误:

错误

'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)
这是的文档,这是我试图获取访问令牌的代码

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
但是VendAPI说以“application/x-www-form-urlencoded”的形式发送post数据

注意这些参数应作为“application/x-www-form-urlencoded”发送 POST请求的编码体

我做错了什么?

在这里发布问题后解决了问题

问题是,我试图使用
application/x-www-form-urlencode
content-type发布数据,但我发送的是json格式的数据。我已经从调用函数中删除了这几行

$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'];
并设置
curl\u setopt($ch,CURLOPT\u POSTFIELDS,$data)$body
数组之外,我还创建了字符串:

$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);

一切正常:)

谢谢!下面是一个使用您的答案后对我有效的完整示例:

但是,发送的头仍然是内容类型:application/json

$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'];
$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);