『壹』 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);