‘壹’ php 匹配http://开头 正则表达
http:\/\/([\w\d]+\.)+\.\w{2}
‘贰’ php实现httpRequest的方法
这篇文章主要介绍了php实现httpRequest的方法,涉及php操作http的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php实现httpRequest的方法。分享给大家供大家参考。具体如下:
想从学校图书馆的网站上抓取数据处理之后在返回给浏览器,试了不少方法。首先试了http_request(),但是这个学院pecl_http支持,后来又试了网上流传甚广的class
HttpRequest,可能是我不会使用,也失败了。后来看到了函数httpRequest($url,
$post='',
$method='GET',
$limit=0,
$returnHeader=FALSE,
$cookie='',
$bysocket=FALSE,
$ip='',
$timeout=15,
$block=TRUE),用它成功了,因此贴出来分享一下。函数代码如下:
代码如下:
<?php
/**
*
Respose
A
Http
Request
*
*
@param
string
$url
*
@param
array
$post
*
@param
string
$method
*
@param
bool
$returnHeader
*
@param
string
$cookie
*
@param
bool
$bysocket
*
@param
string
$ip
*
@param
integer
$timeout
*
@param
bool
$block
*
@return
string
Response
*/
function
httpRequest($url,$post='',$method='GET',$limit=0,$returnHeader=FALSE,$cookie='',$bysocket=FALSE,$ip='',$timeout=15,$block=TRUE)
{
$return
=
'';
$matches
=
parse_url($url);
!isset($matches['host'])
&&
$matches['host']
=
'';
!isset($matches['path'])
&&
$matches['path']
=
'';
!isset($matches['query'])
&&
$matches['query']
=
'';
!isset($matches['port'])
&&
$matches['port']
=
'';
$host
=
$matches['host'];
$path
=
$matches['path']
?
$matches['path'].($matches['query']
?
'?'.$matches['query']
:
'')
:
'/';
$port
=
!empty($matches['port'])
?
$matches['port']
:
80;
if(strtolower($method)
==
'post')
{
$post
=
(is_array($post)
and
!empty($post))
?
http_build_query($post)
:
$post;
$out
=
"POST
$path
HTTP/1.0rn";
$out
.=
"Accept:
*/*rn";
//$out
.=
"Referer:
$boarrlrn";
$out
.=
"Accept-Language:
zh-cnrn";
$out
.=
"Content-Type:
application/x-www-form-urlencodedrn";
$out
.=
"User-Agent:
$_SERVER[HTTP_USER_AGENT]rn";
$out
.=
"Host:
$hostrn";
$out
.=
'Content-Length:
'.strlen($post)."rn";
$out
.=
"Connection:
Closern";
$out
.=
"Cache-Control:
no-cachern";
$out
.=
"Cookie:
$cookiernrn";
$out
.=
$post;
}
else
{
$out
=
"GET
$path
HTTP/1.0rn";
$out
.=
"Accept:
*/*rn";
//$out
.=
"Referer:
$boarrlrn";
$out
.=
"Accept-Language:
zh-cnrn";
$out
.=
"User-Agent:
$_SERVER[HTTP_USER_AGENT]rn";
$out
.=
"Host:
$hostrn";
$out
.=
"Connection:
Closern";
$out
.=
"Cookie:
$cookiernrn";
}
$fp
=
fsockopen(($ip
?
$ip
:
$host),
$port,
$errno,
$errstr,
$timeout);
if(!$fp)
return
'';
else
{
$header
=
$content
=
'';
stream_set_blocking($fp,
$block);
stream_set_timeout($fp,
$timeout);
fwrite($fp,
$out);
$status
=
stream_get_meta_data($fp);
if(!$status['timed_out'])
{//未超时
while
(!feof($fp))
{
$header
.=
$h
=
fgets($fp);
if($h
&&
($h
==
"rn"
||
$h
==
"n"))
break;
}
$stop
=
false;
while(!feof($fp)
&&
!$stop)
{
$data
=
fread($fp,
($limit
==
0
||
$limit
>
8192
?
8192
:
$limit));
$content
.=
$data;
if($limit)
{
$limit
-=
strlen($data);
$stop
=
$limit
<=
0;
}
}
}
fclose($fp);
return
$returnHeader
?
array($header,$content)
:
$content;
}
}
?>
调用也很简单的。简单的例子:
代码如下:
echo
httpRequest('http://www..com');
希望本文所述对大家的php程序设计有所帮助。
‘叁’ php哪些方式发送http请求
第一种实现方式:实用socket编程,通常我们实用fsockopen这个函数来创建一个socket连接,用fputs来发送一个请求
第二种实现方式:实用php的curl扩展,我们使用curl_init()来初始化一个连接,然后设置一堆的curl_setopt()的东西来设置url,post的数据等等,最后我们使用curl_exec()来实现请求。
第三种方式就是: 实用file_get_contents函数,其实我们平时抓取一个网页可能只实用它的第一个参数,其实它的第三个参数就有数据了
‘肆’ php怎么响应客户端发送http请求
http请求有get,post。
php发送http请求有三种方式[我所知道的有三种,有其他的告诉我]。
1. file_get_contents();详情见:http://www.jb51.net/article/41833.htm
2. curl发送请求。
3. fsocket发送。
下面说使用curl发送。
首先环境需要配置好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。
‘伍’ 6 个开源的PHP HTTP 客户端请求库
1. Guzzle:
Guzzle is an independent HTTP client for PHP. You no longer need to depend on cURL, SOAP or REST to pull the data. Guzzle serves need of API for communication in old static/dynamic websites. The new websites are mostly dependent on front-end technology. Guzzle is very time saving option for specific projects.
2. Requests:
Requests for PHP humble HTTP request library. It simplifies from you interact with other sites and takes away all your worries. Requests can provide better API wrapper than cURL. Many developers have liked the idea of Requests and they are migrating to Requests for many of their projects.
3. HTTPFul:
HTTPFul is simple, chain able and readable PHP library intended to make speaking HTTP sane. Developers focus on interacting with APIs installing sifting through cURL set_opt pages. This is an ideal PHP REST client. HTTPFul is evolving project. The tool brings many cool features to developers.
4. PHP VCR:
PHP VCR helps you in recording test suit’s HTTP integration and replay them ring future test runs. PHP VCR assures fast, deterministic and accurate tests. The tool can record HTTP interactions automatically and replay them back to you with little code work. It also supports YAML and JSON for storing data. PHP VCR supports all common HTTP extensions and functions.
5. Buzz:
Buzz is a lightweight HTTP client. You an store and retrieve data using Buzz. This is a great tool for beginners, it helps them learn more about HTTP clients and the way they work in real world.
6. Goutte:
Goutte is an innovative HTTP scraping tool. You can use Goutte to scrape content using PHP skills. You can integrate the Goutte API to scrape/screen websites and code some stuff to extract data as per your requirements.
‘陆’ php http请求方法有什么区别
php http请求的三种方法
方法一:利用php的socket编程来直接给接口发送数据来模拟post的操作。
方法二:使用PHP的curl扩展或HttpClient.class.php类
方法三:这个要借助第三方类库HttpClient
‘柒’ php怎么发送http请求并接收返回值
可以用curl
functionmycurl($url){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;.NETCLR1.1.4322;.NETCLR2.0.50727)");
$res=curl_exec($ch);
curl_close($ch);
return$res;
}
$url='';//http://www.dengguoquan.com/gather/
$dd=mycurl($url);
print_r($dd);