猿问

curl post发送超过1024byte的数据,怎么发送才能保证数据的完整性?

代码:

$data = $_REQUEST['data'];
$curl = curl_init();
$data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABYAAAAeCAYAAAAo5+5WAAAAwUlEQVRIie2UwQkCMRBFf8RSdj2IFWQrMCdPlpA0YRHZqyV42lPSiLfYy5i4oIhEJ0pAdD/M7fP4Gf5EUBQqaFYDOoG/AOyNgBC36frT5+AEHTaE1MZxAraHFsYzyVSiYElqx7KW7bhZYMW0loH9gOOyZVnnBVQYBeyo4dlZC3M6/ieaeNsd9TJxaoeCiwHW/Mc9TxzIShCzBA/Kgp2WZMN70Dy4oK855eu2V3fnfJ2uB+ewxSV2Bf3YtzmBJ/C/g88GTM7oHPlgrAAAAABJRU5ErkJggg==";
curl_setopt_array($curl, array(
    CURLOPT_URL => "http://domain.com",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POSTFIELDS => "type=webimage&image={$data}",
    CURLOPT_HTTPHEADER => array(
        "content-type: application/x-www-form-urlencoded"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);
if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

但是服务端接收数据不全,导致Base64编码的图片无法解析。

月关宝盒
浏览 557回答 1
1回答

梵蒂冈之花

因为你这里指定了x-www-form-urlencoded,所以你传输的数据必须urlencode: $data = urlencode($data);
随时随地看视频慕课网APP
我要回答