PHP curl_setopt_array不发送post prams数据

时间:2017-10-16 10:50:06

标签: php curl php-5.6 php-curl

在PHP 5.6中, 当我尝试使用curl_setopt_array发送帖子数据时,发布数据不发送。但是当我使用curl_setopt

时会发送数据

1。 POST数据和卷曲参数:

$url = "https://XXXXXXXXXXX/oauth/v2/accessToken";

$data = array(
    'grant_type' => 'authorization_code',
    'code'=>$_REQUEST['code'],
    'redirect_uri'=>$redirect_uri,
    'client_id'=>$client_id,
    'client_secret'=>$client_secret
 );

$header = array('Content-Type: application/ x-www-form-urlencoded');

$options = array( 
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_URL => $url,
    CURLOPT_POST=>true,
    CURLOPT_RETURNTRANSFER => true,
    // CURLOPT_SSL_VERIFYPEER => false
);

2.我的“curl_setopt_array”代码:

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

var_dump( $json);

3.我的“curl_setopt”代码

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode( $response,true);

var_dump($response);

请注意: 根据我的知识,我的英语不是很好。

输出:

代表“CURL_SETOPT_ARRAY”代码:

string(95)“{”error“:”invalid_request“,”error_description“:”必需参数\“client_id \”缺少“}”

代表“CURL_SETOPT”代码: 在json解码后:     array(2){[“access_token”] =>串(179) “AQWXnJKB5enEyPcqY68ldCUyWayz2LsNXhp-9E-RBaEwGX8EyB5rIDL6YcZh6k_kPT9vJbo_xAQUCeu4hJjeqJTFmTjuFhI3rReHCF4xYBeqGdA7L5_HXSqmdk6oZXtcRSqvCunp2HYsaoLpnCUeFFeYIF5MchaRXl8P_OjXERC3J7XHDU0”[ “expires_in”] => int(5183999)}

1 个答案:

答案 0 :(得分:0)

您正在与之交互的API可能需要JSON而不是URLENCODED数据,请尝试将http_build_query替换为json_encode并告知我们是否有帮助! :)