php Curl 里面模拟表单提交 文本+文件的写法

模拟文本:

$username='admin';
$password='admin';
$fp=fsockopen('www.pooy.net','80',$errno,$errstr, 1);
 
$post="username=$username&password=$password";
if(!$fp) {
    echo"$errstr ($errno)<br />\n";
}else{
    $out="POST /SJzgImgEdit/login HTTP/1.1\r\n";
    $out.="Host: www.pooy.net\r\n";
    $out.='User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 FirePHP/0.6'."\r\n";
    $out.='Content-Type: application/x-www-form-urlencoded' ."\r\n";
    $out.='Content-Length: '.strlen($post)."\r\n";
    $out.="Connection: Close\r\n\r\n";
    $out.=$post."\r\n";
    fwrite($fp,$out);
    while(!feof($fp)) {
        echofgets($fp, 128);  
    }
    fclose($fp);
}



CURL POST 上传文件(模拟页面)



define('UPLOAD_IMG','http://www.pooy.net/Snoopy/upload.php');
functionupload_curl_pic($file)
{
    $url = UPLOAD_IMG;
    $fields['f'] = '@'.$file;
    $ch= curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    
    ob_start();
    curl_exec($ch);
    $result= ob_get_contents();
    ob_end_clean();
    curl_close($ch);
    return$result;
}
$file= 'H:\www\test\psuCARGLSPA-pola.jpg';//要上传的文件
$src= upload_curl_pic($file);
echo$src;




CURL POST 上传文件(处理页面)



header("content-type:text/html;charset=utf-8");
    //防止上传
 
        //$file=$_FILES['f'];
    //var_dump($file);
    exit();
     
    $uploaddir= "H:/www/test/Snoopy/test/";
    $uploadfile= $uploaddir. $_FILES['f']['name'];
    if(move_uploaded_file($_FILES['f']['tmp_name'],$uploadfile))
    {
         echo$uploadfile;
         //echo "File is valid, and was successfully uploaded.\n";
    }
    else
    {
        echo'0';
        //echo "Possible file upload attack!\n";
        //echo 'Here is some more debugging info:';
    }



转载:http://www.oschina.net/question/553727_69528


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值