标签:
这是简单的将一个变量post到另外一个页面
$url = ‘‘;
$data = array(‘a‘=> ‘b‘);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$ret = curl_exec($ch);
curl_close($ch);
主要说下这个选项CURLOPT_RETURNTRANSFER:如果设置为true/1,则curl_exec的时候不会自动将请求网页的内容输出到屏幕,$ret为请求网页的内容,如果设置为false/0,则curl_exec的时候会自动将请求网页的内容输出到屏幕,此时如果请求成功的话$ret的内容是1或者true。
下面是上传本地文件的代码,如果需要上传远程文件,则先down到本地,然后删掉即可(如有同学有别的办法还请告知):
$url = ‘‘;
$file = ‘1.jpg‘;
$field[‘uploadFile‘] = ‘@‘.$file;//(uploadFile为接收端的name名)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $field);
$ret = curl_exec($ch);
curl_close($ch);
-------------------------------------------------------------------------------------
这是fsockopen的办法:
$uploadInfo = array(
‘host‘=>‘‘,
‘port‘=>‘80‘,
‘url‘=>‘/upload.php‘
);
$fp = fsockopen($uploadInfo[‘host‘],$uploadInfo[‘port‘],$errno,$errstr);
$file = ‘1.jpg‘;
$content = file_get_contents($file);
$boundary = md5(time());
$out.="--".$boundary."\r\n";
$out.="Content-Disposition: form-data; name=\"uploadFile\"; filename=\"".$file."\"\r\n";
$out.="Content-Type: image/jpg\r\n\r\n";
$out.=$content."\r\n";
$out.="--".$boundary."\r\n";
fwrite($fp,"POST ".$uploadInfo[‘url‘]." HTTP/1.1\r\n");
fwrite($fp,"Host:".$uploadInfo[‘host‘]."\r\n");
fwrite($fp,"Content-Type: multipart/form-data; boundary=".$boundary."\r\n");
fwrite($fp,"Content-length:".strlen($out)."\r\n\r\n");
fwrite($fp,$out);
while (!feof($fp)){
$ret .= fgets($fp, 1024);
}
fclose($fp);
$ret = trim(strstr($ret, "\r\n\r\n"));
preg_match(‘/http:.*/‘, $ret, $match);
return $match[0];
标签:
原文地址:http://www.cnblogs.com/zhouguowei/p/5314733.html