php中curl、fsockopen的应用
时间:2024年02月09日
/来源:网络
/编辑:佚名
最近要用到通过post上传文件,网上盛传的有curl的post提交和fsockopen,其中curl最简单,于是从最简单的说起。
这是简单的将一个变量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];
这是简单的将一个变量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];
新闻资讯 更多
- 【建站知识】查询nginx日志状态码大于400的请求并打印整行04-03
- 【建站知识】Python中的logger和handler到底是个什么?04-03
- 【建站知识】python3拉勾网爬虫之(您操作太频繁,请稍后访问)04-03
- 【建站知识】xpath 获取meta里的keywords及description的方法04-03
- 【建站知识】python向上取整以50为界04-03
- 【建站知识】scrapy xpath遇见乱码解决04-03
- 【建站知识】scrapy爬取后中文乱码,解决word转为html 时cp1252编码问题04-03
- 【建站知识】scrapy采集—爬取中文乱码,gb2312转为utf-804-03