源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

php发送http请求的常用方法分析

  • 时间:2020-11-17 16:31 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:php发送http请求的常用方法分析
本文实例讲述了php发送http请求的常用方法。分享给大家供大家参考,具体如下: [b]http请求有get,post[/b]。 php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。 1. file_get_contents();详情见:[url=http://www.1sucai.cn/article/41833.htm]http://www.1sucai.cn/article/41833.htm[/url] 2. curl发送请求。 3. fsocket发送。 下面说[b]使用curl发送[/b]。 首先环境需要配置好curl组件。 在windows中让php支持curl比较简单: 在php.ini中将extension=php_curl.dll前面的分号去掉, 有人说需要将php根目录的libeay32.dll和ssleay32.dll需要拷贝到系统目录下去。我实验不拷贝也可以。 在linux中,如果使用源码安装,需要在make 之前,./configure --with-curl=path, 其中,path是你的 libcurl库的位置,比如你安装libcurl库之后, path可能就是/usr/local/,libcurl可以是静态库,也可以是动态库。 注意libcurl库configure的时候,可以将一些不需要的功能去掉, 比如ssl , ldap等。在php configure的时候,会去检查libcurl中某些功能是否被开启,进而去相应地调整生成的php 两个使用curl发请求的例子。
// 初始化一个 cURL 对象
$curl = curl_init();
// 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, 'http://www.1sucai.cn');
// 设置header 响应头是否输出
curl_setopt($curl, CURLOPT_HEADER, 1);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
// 1如果成功只将结果返回,不自动输出任何内容。如果失败返回FALSE
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
// 运行cURL,请求网页
$data = curl_exec($curl);
// 关闭URL请求
curl_close($curl);
// 显示获得的数据
print_r($data);

再一个post方式的例子:
//post方式
$phoneNumber ="13912345678";
$message = "testMessage";
$curlPost = "phone=".urlencode($phoneNumber)."&message=".$message;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://mytest/lab/t.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
//设置是通过post还是get方法
curl_setopt($ch,CURLOPT_POST,1);
//传递的变量
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
$data = curl_exec($ch);
curl_close($ch);

在这个http://mytest/lab/t.php文件中:
if(!empty($_POST)){
 print_r($_POST);
}

就先写这么多。 [b]Fsocket:[/b]
$gurl = "http://mytest/lab/t.php?uu=gggggg";
//print_r(parse_url($gurl));
echo "以下是GET方式的响应内容:<br>";
sock_get($gurl);
function sock_get($url)
{
 $info = parse_url($url);
 $fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
 $head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0\r\n";
 $head .= "Host: ".$info['host']."\r\n";
 $head .= "\r\n";
 $write = fputs($fp, $head);
 while (!feof($fp)){
  $line = fgets($fp);
  echo $line."<br>";
 }
}
//fsocket模拟post提交
$purl = "http://mytest/lab/t.php";
echo "以下是POST方式的响应内容:<br>";
sock_post($purl,"uu=rrrrrrrrrrrr&&kk=mmmmmm");
function sock_post($url, $query)
{
 $info = parse_url($url);
 $fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
 $head = "POST ".$info['path']." HTTP/1.0\r\n";
 $head .= "Host: ".$info['host']."\r\n";
 $head .= "Referer: http://".$info['host'].$info['path']."\r\n";
 $head .= "Content-type: application/x-www-form-urlencoded\r\n";
 $head .= "Content-Length: ".strlen(trim($query))."\r\n";
 $head .= "\r\n";
 $head .= trim($query);
 $write = fputs($fp, $head);
 print_r(fgets($fp));
 while (!feof($fp))
 {
  $line = fgets($fp);
  echo $line."<br>";
 }
}

curl添加gzip的参数可参考: php curl中gzip的压缩性能测试实例分析: [url=http://www.1sucai.cn/article/96778.htm]http://www.1sucai.cn/article/96778.htm[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/66.htm]php curl用法总结[/url]》、《[url=http://www.1sucai.cn/Special/70.htm]php socket用法总结[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部