php curl设置请求Content-Type

function request_post($url = '', $request_data = '')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $request_data);
        $data = curl_exec($ch);
        curl_close($ch);

        return json_decode($data, true);
    }
  • application/x-www-form-urlencoded模式:
    -- $request_data 是字符串,则Content-Type是application/x-www-form-urlencoded。
$request_data = [
            'name' => ‘name’,
            'sex' => ‘sex’,
        ];
$request_data = json_encode($request_data);
  • multipart/form-data模式:
    -- $request_data 是数组,则Content-Type是multipart/form-data。
$request_data = [
            'name' => ‘name’,
            'sex' => ‘sex’,
        ];

你可能感兴趣的:(php curl设置请求Content-Type)