PHP CURL 模拟POST请求 提交数据或上传文件

时间:2022-10-16 20:59:12

1.http://www.a.com/a.php

发送POST请求

function execUpload(){


$file = '/doucment/Readme.txt';
$ch = curl_init();
$post_data = array(
    'loginfield' => 'username',
    'username' => 'ybb',
    'password' => '123456',
'file' => '@d:\usr\www\translate\document\Readme.txt'
);
curl_setopt($ch, CURLOPT_HEADER, false);
//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_URL, 'http://www.b.com/handleUpload.php');
$info= curl_exec($ch);
curl_close($ch);
   
print_r($info);

}

2.http://www.b.com/handleUpload.php

function handleUpload(){
print_r($_POST);
echo '===file upload info:';
print_r($_FILES);
}