簡體   English   中英

PHP Curl 在 post 請求中發送了錯誤的 Content-Type

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

我正在嘗試使用 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

我相信我發送的一切都很好,但是 curl 正在發送但是從 curl 信息我得到了這個

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

但是 VendAPI 文檔說將發布數據作為"application/x-www-form-urlencoded" 發送

注意這些參數應該作為 POST 請求的“application/x-www-form-urlencoded”編碼主體發送

我做錯了什么?

在這里發布問題后解決了問題。

問題是,我正在嘗試使用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'];

一切正常:)

謝謝! 這是使用您的答案后對我有用的完整示例:

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

但是,發送的標頭仍然是 Content-Type: application/json。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM