简体   繁体   English

PHP Curl 在 post 请求中发送了错误的 Content-Type

[英]PHP Curl sent wrong Content-Type in post request

I am trying to sent a Post Request to VendHQ Api using oauth2 auth method.我正在尝试使用 oauth2 auth 方法向 VendHQ Api 发送 Post 请求。 I've the correct code, client_id, client_secret etc as It work fine in Postman but when I try to sent the same data using PHP curl, I get the error:我有正确的代码、client_id、client_secret 等,因为它在 Postman 中工作正常,但是当我尝试使用 PHP curl 发送相同的数据时,出现错误:

Error错误

'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)

This is the documentation of Requesting Access Token and this is my code which is trying to get the access token.这是请求访问令牌的文档,这是我试图获取访问令牌的代码。

PHP Code: 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);

Invoke function调用函数

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

I believe I am sending everything fine but the curl is sending but from curl info I get this我相信我发送的一切都很好,但是 curl 正在发送但是从 curl 信息我得到了这个

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

But VendAPI Documentation says to send post data as "application/x-www-form-urlencoded" .但是 VendAPI 文档说将发布数据作为"application/x-www-form-urlencoded" 发送

NOTE Those parameters should be sent as “application/x-www-form-urlencoded” encoded body of a POST request注意这些参数应该作为 POST 请求的“application/x-www-form-urlencoded”编码主体发送

What I am doing wrong?我做错了什么?

Solved the problem after posting the question here.在这里发布问题后解决了问题。

The problem is, I am trying to post the data with application/x-www-form-urlencode content type but I was sending data in json format.问题是,我正在尝试使用application/x-www-form-urlencode内容类型发布数据,但我以 json 格式发送数据。 I've removed this lines from invoke function我已经从调用函数中删除了这一行

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

and set curl_setopt($ch, CURLOPT_POSTFIELDS, $data);并设置curl_setopt($ch, CURLOPT_POSTFIELDS, $data); and except creating a $body array, I've created string:除了创建一个$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'];

All worked fine :)一切正常:)

Thanks!谢谢! Here is a complete example that worked for me after using your answer:这是使用您的答案后对我有用的完整示例:

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

However, the header sent is still the Content-Type: application/json.但是,发送的标头仍然是 Content-Type: application/json。

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

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