PHP Curl sent wrong Content-Type in post request -


i trying sent post request vendhq api using oauth2 auth method. i've correct code, client_id, client_secret etc work fine in postman when try sent same data using php curl, error:

error

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

this documentation of requesting access token , code trying access token.

php code:

$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 sending fine curl sending curl info this

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

but vendapi documentation says send post data "application/x-www-form-urlencoded".

note parameters should sent “application/x-www-form-urlencoded” encoded body of post request

what doing wrong?

solved problem after posting question here.

the problem is, trying post data application/x-www-form-urlencode content type sending data in json format. i've removed lines invoke function

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

and set curl_setopt($ch, curlopt_postfields, $data); , except creating $body array, i've created string:

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


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -