PHP cURL不发送$ _POST数据

时间:2019-05-16 11:11:40

标签: php curl

我有一个要解决的问题,具体而言,我尝试使用cURL调用广告API。 但是我的php代码没有发送$ _POST数据。

$service_url = 'https://localhost/api/auth/authenticate';
$curl = curl_init($service_url);

$fields = [
    'username' => urlencode('g.pappalardo'),
    'password' => urlencode('roccerirva48'),
];

curl_setopt($curl, CURLOPT_POST,  count($fields));
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header = [
        'accept: application/json', 
        'Content-Type' => 'application/x-www-form-urlencoded',
        'charset=UTF-8',
        "content-transfer-encoding: json \r\n",
        ]);

    $curl_response = curl_exec($curl);

    if ($curl_response === false) {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occured during curl exec. Additioanl info: ' . var_export($info));
    }

    curl_close($curl);
    $decoded = json_decode($curl_response);
    if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
        die('error occured: ' . $decoded->response->errormessage);
    }
    echo '<br><br>';
    print_r($_POST);

我希望$ fields数组将作为发布数据传递,但是当我在$ _POST中使用print_r时,该数组显示为空。

0 个答案:

没有答案