#
國(guó)外的開(kāi)源技術(shù)也影響和推動(dòng)了國(guó)內(nèi)開(kāi)源程序的發(fā)展,上文我介紹的《國(guó)外優(yōu)秀開(kāi)源PHP建站程序一覽》中,很多國(guó)外開(kāi)源程序并不太符合中國(guó)人的使用習(xí)慣,而國(guó)內(nèi)有一些廠家或個(gè)人也做了一些不錯(cuò)的產(chǎn)品,不少程序是提供源代碼下載的,雖然有些在許可協(xié)議上和開(kāi)源許可證有些出入,但其在使用上還是挺符合中國(guó)人的使用習(xí)慣,今天我就介紹一些國(guó)內(nèi)的PHP“開(kāi)源”建站程序。
論壇:Discuz
Discuz非常流行,也是中國(guó)地區(qū)最多用戶(hù)使用的論壇程序,論壇搭建非常簡(jiǎn)單易用,使用風(fēng)格符合中國(guó)人的口味。另一個(gè)流行的論壇程序是PHPWind。
SNS:Ucenter Home
UCenter Home是采用PHP+MySQL構(gòu)建的社會(huì)化網(wǎng)絡(luò)軟件(Social Network Software,簡(jiǎn)稱(chēng)SNS)。 通過(guò) UCenter Home,建站者可以輕松構(gòu)建一個(gè)以好友關(guān)系為核心的交流網(wǎng)絡(luò),用戶(hù)可以使用迷你博客記錄;方便快捷地發(fā)布日志、上傳圖片;與其好友們一起分享信息、討論話題;了解好友最新動(dòng)態(tài)。
E-Commerce:ECSHOP
ECSHOP是一款開(kāi)源免費(fèi)的網(wǎng)上商店系統(tǒng),用戶(hù)可以根據(jù)自己的商務(wù)特征對(duì)ECSHOP進(jìn)行定制,增加自己商城的特色功能。另一個(gè)流行的網(wǎng)上商城系統(tǒng)是shopex。
點(diǎn)評(píng):Modoer
Modoer一款PHP點(diǎn)評(píng)系統(tǒng),可針對(duì)多種行業(yè)進(jìn)行點(diǎn)評(píng),可以自由調(diào)控點(diǎn)評(píng)項(xiàng)目,類(lèi)型,采用Web 2.0的建站方式,網(wǎng)站會(huì)員能讓快速上手。
Digg:PBDigg
PBDigg是基于PHP+MYSQL的開(kāi)源Digg社區(qū)資訊系統(tǒng),融合了社會(huì)性標(biāo)簽、主題評(píng)論、Rss訂閱等多種WEB2.0元素,是一個(gè)高效、快速的網(wǎng)站解決方案。
Wiki:HDWiki
HDWiki是專(zhuān)為中文用戶(hù)設(shè)計(jì)和開(kāi)發(fā)的開(kāi)源、高效的中文百科建站解決方案,免費(fèi)、易用、功能強(qiáng)大,和UCenter可無(wú)縫整合。
RSS:IXNA
IXNA是國(guó)內(nèi)開(kāi)源PHP新聞聚合程序,支持RSS多核心切換,默認(rèn)支持lastrss、simplepie、magpierss,支持RDF、RSS、ATOM,支持智能識(shí)別。
CMS:關(guān)于CMS的PHP產(chǎn)品很多,這里主要介紹下面三個(gè)。
SupeSite
SupeSite 是一套獨(dú)立的內(nèi)容管理系統(tǒng)(CMS),并且擁有對(duì)Discuz!論壇信息和UCenter Home個(gè)人空間信息聚合的功能,是一個(gè)不錯(cuò)的社區(qū)門(mén)戶(hù)解決方案。
DedeCMS
DedeCMS是一個(gè)比較老的PHP CMS系統(tǒng),很多早期的用戶(hù)都是使用這個(gè)建立網(wǎng)站的。
KingCMS
KingCMS是一套簡(jiǎn)單易學(xué),操作簡(jiǎn)單的開(kāi)源內(nèi)容管理系統(tǒng)(CMS),KingCMS分為PHP+MySQL和ASP+MSSQL/ACCESS兩種語(yǔ)言版本的系統(tǒng)。
最后是一次快訊,Twitter又無(wú)法訪問(wèn)了,估計(jì)XJ那里完事后可能會(huì)恢復(fù),現(xiàn)在的解決方法在這里。
大量的PHP開(kāi)源(開(kāi)放源代碼/Open Source)應(yīng)用改變了這個(gè)世界,改變了互聯(lián)網(wǎng),以下我們總結(jié)從數(shù)據(jù)庫(kù)到購(gòu)物、博客等眾多類(lèi)型的開(kāi)源PHP軟件,供網(wǎng)站開(kāi)發(fā)者們參考。
博客:WordPress
WordPress是使用PHP開(kāi)發(fā)的著名博客平臺(tái),免費(fèi)開(kāi)源,功能強(qiáng)大,不僅僅用于博客搭建,還可以廣泛應(yīng)用于各類(lèi)網(wǎng)絡(luò)信息發(fā)布平臺(tái)。
論壇:phpBB
phpBB是一種廣泛流行的開(kāi)源論壇軟件,具有易于使用的管理面板和友好的用戶(hù)安裝界面,可以輕松地在數(shù)分鐘內(nèi)建立起一個(gè)論壇,功能上具有很高的可配置性,能夠完全定制出相當(dāng)個(gè)性化的論壇。
CMS:Drupal
Drupal是一個(gè)開(kāi)源的內(nèi)容管理系統(tǒng)(CMS)平臺(tái),擁有強(qiáng)大并可自由配置的功能,能支持從個(gè)人博客到大型社區(qū)驅(qū)動(dòng)的網(wǎng)站等各種不同應(yīng)用的網(wǎng)站項(xiàng)目。
Wiki:MediaWiki
MediaWiki是PHP語(yǔ)言寫(xiě)成開(kāi)源Wiki引擎,全世界最大的Wiki項(xiàng)目維基百科就是使用MediaWiki引擎。
Digg:Pligg
Pligg是一套靈活的類(lèi)似Digg的Web2.0 CMS系統(tǒng),系統(tǒng)使用PHP開(kāi)發(fā),模仿了國(guó)外流行的DIGG系統(tǒng)。
圖像:Gallery
Gallery 是一個(gè)非常有名的免費(fèi)開(kāi)源圖庫(kù)相冊(cè)軟件,基于 PHP 和 MySQL, PostgreSQL 等數(shù)據(jù)庫(kù)。功能非常強(qiáng)大,有豐富的擴(kuò)展可以下載,安裝很簡(jiǎn)單,有很多插件可用。
RSS:Gregarius
Gregarius是一個(gè)RSS聚合程序,免費(fèi)開(kāi)源,具備不錯(cuò)的用戶(hù)體驗(yàn),易于操作和管理。可以把其當(dāng)成RSS閱讀器使用。
電子商務(wù):osCommerce
osCommerce是一套由自由軟件開(kāi)發(fā)社團(tuán)開(kāi)發(fā)并維護(hù)的在線商店的解決方案,免費(fèi)開(kāi)源,并可以應(yīng)用到任何的商業(yè)環(huán)境中,可以在短時(shí)間內(nèi)生成一個(gè)功能強(qiáng)大的電子商務(wù)網(wǎng)站。
廣告:OpenX
OpenX(原名phpAdsNew)是一個(gè)用PHP開(kāi)發(fā)的廣告管理與跟蹤系統(tǒng),適合各類(lèi)網(wǎng)站使用,能夠管理每個(gè)廣告主擁有的多種任何尺寸橫幅廣告,按天查看,詳細(xì)和概要統(tǒng)計(jì)并通過(guò)電子郵件發(fā)送報(bào)表給廣告主。
微博客:Laconica
Laconica是一個(gè)開(kāi)源的微型博客系統(tǒng),也是一個(gè)Twitter克隆,可以實(shí)現(xiàn)Microblog的常用功能,國(guó)外不少微博客系統(tǒng)都是通過(guò)這個(gè)開(kāi)源系統(tǒng)架設(shè)的。
英文原文:Top Ten Open Source PHP Apps
1 JS方式調(diào)用PHP文件并取得php中的值
舉一個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明:
如在頁(yè)面a.html中用下面這句調(diào)用:
<script type="text/javascript" src="b.php?action=test"></script>
<script type="text/javascript" >
alert(jstext);
</script>
在b.php中有這樣一段PHP代碼:
<? $action=$_GET['action']; //echo "var jstext='$action'"; //輸出一句JS語(yǔ)句,生成一個(gè)JS變量,并賦顛值為PHP變量 $action的值 //echo "var jstext='aa'"; echo "var jstext="."'$action'"; ?>
當(dāng)執(zhí)行a.html文件時(shí),就會(huì)調(diào)用b.php文件,并將b.php文件的輸出作為JS語(yǔ)句來(lái)執(zhí)行,所以此處會(huì)彈出一個(gè)提示框,內(nèi)容為JS變量jstext的值,也就是在PHP文件中賦給jstext的值.
小結(jié):
在HTML里用JS 調(diào)用文件的方式調(diào)PHP文件,則PHP文件的輸出將會(huì)被調(diào)用頁(yè)作為JS的代碼來(lái)用.
2 php調(diào)用js中的值
在z.php頁(yè)面中有這樣一段代碼:
<script type="text/javascript" > var url="aaaa*"; </script> <? $key="<script type=text/javascript>document.write(url)</script>"; echo $key; ?>
3 php調(diào)用js中的方法(函數(shù))
<script type="text/javascript"> function test() { var t1=3; t1 = t1+2; alert(t1); //return t1; } </script>
<?php echo "<script type='text/javascript'>test();</script>"; ?>
4 JS調(diào)用PHP變量
(1)
<?php
$userId=100;
?> <script>
var userId;
userId=document.getElementByIdx_x_x_x("userId").value;
alert (userId);
</script>
<input type="text" name="userId" id="userId" value="<?php echo $userId; ?>">
(2)
<?php
$url = '變化的網(wǎng)址'; //定義變量
?>
<script type="text/javascript">
//js調(diào)用php變量
var ds ="<?php echo $url?>" ; //賦值 alert(ds); //輸出效果 </script>
5 -------------------------------
<script language="JavaScript"> <!--
var Y=<?php echo date('Y')?>,M=<?php echo date('n')?>,D=<?php echo date('j')?>;
-->
</script>
6 自己寫(xiě)的js和php互相調(diào)用
1.php內(nèi)容:
<?php
//echo "<script LANGUAGE='javascript'>alert('$php變量');</script>"; //最簡(jiǎn)單的php調(diào)用js
//echo "<a href=#><img width=50 src='$fruit_pic_array[$i]' onMouseOver=’javascript:a();‘></a>";
//echo "<a href='3.php'>aaaa</a>"; //php中超鏈接
//echo "<script type='text/javascript' language='javascript'>phpmake('PHP建站學(xué)習(xí)筆記網(wǎng)');</script>"; //有時(shí)候需要在PHP執(zhí)行過(guò)程中,需要調(diào)用JavaScript自定義函數(shù)(驗(yàn)證時(shí)出錯(cuò))
echo "function ok(msg){alert(msg);}";
?>
<HTML>
<HEAD>
<TITLE> php調(diào)用js文件的好辦法</TITLE>
</HEAD>
<BODY>
<!--js調(diào)用php中定義的js-->
<scrīpt language=''javascrīpt'' type=''text/javascrīpt'' src=''1.php''></scrīpt>
<scrīpt>
ok("aaaaaa!");
</scrīpt>
</script>
</BODY>
</HTML>
2.php內(nèi)容:
<!--js調(diào)用php-->
<?php
$userId=100;
?>
<script>
var userId;
userId=document.getElementByIdx_x("userId").value;
alert (userId);
</script>
<input type="text" name="userId" id="userId" value="<?php echo $userId; ?>">
<!--js調(diào)用php-->
<?php
if($_GET["action"]=="ok")
{
echo "I'm OK!";
}
else
{
echo "I'm not OK!";
}
?>
<SCRIPT Language = "JavaScript">
function func()
{
if(confirm("Are you OK with this?"))
{
this.location = "ok.php?action=ok";
}
else
{
this.location = "ok.php?action=cancel";
}
}
</SCRIPT>
<html>
<head>
</head>
<body>
<a href="#" href="#" onClick="javascript:func();">Please Click</a>
</body>
</html>
<!--js調(diào)用php-->
<html>
<head>
<script>
function isMail(PostString)
{
re=/\w*/
if(re.test(PostString))
{
return true;
}
else
{
return false;
}
}
function test(){
if (isMail(<?php echo $email?>))
{document.write("<?php echo "N";?>");}
else
{document.write('<?php echo 'Y';?>');}
}
</script>
</head>
<body>
<?php
$email="aa";
?>
<input type=button value=click onclick= 'test() '>
</body>
</html>
<!--php中含有js代碼-->
<?php
echo "
<script language=javascript>
function test(){
alert( 'hello ');
}
</script> ";
?>
<input type=button value=click onclick= 'test() '>
摘要: 下面的例子列出幾種情形交互場(chǎng)景,列出JS和php交互的方法。總結(jié)下,以免日后再為cookie問(wèn)題困擾。setcookie.php01<?php02 setcookie('php_cn_ck','php_中文_cookie');03 setcookie('php_en_ck','php_english_...
閱讀全文
<?php
/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('facebook.cn','/restServer.php',$post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){
$socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket,"POST $remote_path HTTP/1.0\r\n");
fwrite($socket,"User-Agent: Socket Example\r\n");
fwrite($socket,"HOST: $remote_server\r\n");
fwrite($socket,"Content-type: application/x-www-form-urlencoded\r\n");
fwrite($socket,"Content-length: ".strlen($post_string)+8."\r\n");
fwrite($socket,"Accept:*/*\r\n");
fwrite($socket,"\r\n");
fwrite($socket,"mypost=$post_string\r\n");
fwrite($socket,"\r\n");
$header = "";
while ($str = trim(fgets($socket,4096))) {
$header.=$str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket,4096);
}
return $data;
}
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_curl($remote_server,$post_string){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$remote_server);
curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_other($remote_server,$post_string){
$context = array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n".
'User-Agent : Jimmy\'s POST Example beta'."\r\n".
'Content-length: '.strlen($post_string)+8,
'content'=>'mypost='.$post_string)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server,FALSE,$stream_context);
return $data;
}
function curl_file_get_contents($durl) {
$ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $durl ); curl_setopt ( $ch, CURLOPT_POST, 1 ); curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 ); curl_setopt ( $ch, CURLOPT_USERAGENT, _USERAGENT_ ); curl_setopt ( $ch, CURLOPT_REFERER, _REFERER_ ); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); $r = curl_exec ( $ch ); curl_close ( $ch ); return $r; }
php讀取網(wǎng)絡(luò)文件 curl, fsockopen ,file_get_contents 幾個(gè)方法的效率對(duì)比
最近需要獲取別人網(wǎng)站上的音樂(lè)數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會(huì)遇到獲取失敗的問(wèn)題,盡管按照手冊(cè)中的 例子設(shè)置了超時(shí),可多數(shù)時(shí)候不會(huì)奏效:
$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”, ’timeout’ => 5//這個(gè)超時(shí)時(shí)間不穩(wěn)定,經(jīng)常不奏效 ) ));這時(shí)候,看一下服務(wù)器的連接池,會(huì)發(fā)現(xiàn)一堆類(lèi)似的錯(cuò)誤,讓我頭疼萬(wàn)分:file_get_contents(http://***): failed to open stream…現(xiàn)在改用了curl庫(kù),寫(xiě)了一個(gè)函數(shù)替換:function curl_file_get_contents($durl){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $durl); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); curl_setopt($ch, CURLOPT_REFERER,_REFERER_); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $r = curl_exec($ch); curl_close($ch); return $r;}如此,除了真正的網(wǎng)絡(luò)問(wèn)題外,沒(méi)再出現(xiàn)任何問(wèn)題。這是別人做過(guò)的關(guān)于curl和file_get_contents的測(cè)試:file_get_contents抓取google.com需用秒數(shù):
2.31319094
2.303742172.215126043.305538892.30124092curl使用的時(shí)間:
0.68719101
0.646755930.643260.819831130.63956594差距很大?呵呵,從我使用的經(jīng)驗(yàn)來(lái)說(shuō),這兩個(gè)工具不只是速度有差異,穩(wěn)定性也相差很大。建議對(duì)網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦!
看到的其他文章收藏于此===============================
php fsockopen
方法1: 用file_get_contents 以get方式獲取內(nèi)容
<?php
$url='http://www.domain.com/';
$html = file_get_contents($url);
echo $html;
?>
方法2: 用fopen打開(kāi)url, 以get方式獲取內(nèi)容
<?php
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
?>
方法3:用file_get_contents函數(shù),以post方式獲取url
<?php
$data = array ('foo' => 'bar');
$data = http_build_query($data);
$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);
echo $html;
?>
方法4:用fsockopen函數(shù)打開(kāi)url,以get方式獲取完整的數(shù)據(jù),包括header和body
<?php
function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path]."?".$url[query];
echo "Query:".$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = "GET $query HTTP/1.1\r\n";
$request .= "Host: $url[host]\r\n";
$request .= "Connection: Close\r\n";
if($cookie) $request.="Cookie: $cookie\n";
$request.="\r\n";
fwrite($fp,$request);
while()) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//獲取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,"\r\n\r\n");
$body=substr($body,4,strlen($body));
return $body;
}
return false;
}
?>
方法5:用fsockopen函數(shù)打開(kāi)url,以POST方式獲取完整的數(shù)據(jù),包括header和body
<?php
function HTTP_Post($URL,$data,$cookie, $referrer="")
{
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer="111";
// making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="Cookie: $cookie\n";
$request.="\n";
$request.=$data_string."\n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);
return $result;
}
?>
方法6:使用curl庫(kù),使用curl庫(kù)之前,可能需要查看一下php.ini是否已經(jīng)打開(kāi)了curl擴(kuò)展
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
php中 curl, fsockopen ,file_get_contents 三個(gè)函數(shù) 都可以實(shí)現(xiàn)采集模擬發(fā)言 。 三者有什么區(qū)別,或者講究么
趙永斌:
有些時(shí)候用file_get_contents()調(diào)用外部文件,容易超時(shí)報(bào)錯(cuò)。換成curl后就可以.具體原因不清楚
curl 效率比f(wàn)ile_get_contents()和fsockopen()高一些,原因是CURL會(huì)自動(dòng)對(duì)DNS信息進(jìn)行緩存(亮點(diǎn)啊 有我待親測(cè))
范佳鵬:
file_get_contents curl fsockopen
在當(dāng)前所請(qǐng)求環(huán)境下選擇性操作,沒(méi)有一概而論:
具我們公司開(kāi)發(fā)KBI應(yīng)用來(lái)看:
剛開(kāi)始采用:file_get_contents
后來(lái)采用:fsockopen
最后到至今采用:curl
(遠(yuǎn)程)我個(gè)人理解到的表述如下(不對(duì)請(qǐng)指出,不到位請(qǐng)補(bǔ)充)
file_get_contents 需要php.ini里開(kāi)啟allow_url_fopen,請(qǐng)求http時(shí),使用的是http_fopen_wrapper,不會(huì)keeplive.curl是可以的。
file_get_contents()單個(gè)執(zhí)行效率高,返回沒(méi)有頭的信息。
這個(gè)是讀取一般文件的時(shí)候并沒(méi)有什么問(wèn)題,但是在讀取遠(yuǎn)程問(wèn)題的時(shí)候就會(huì)出現(xiàn)問(wèn)題。
如果是要打一個(gè)持續(xù)連接,多次請(qǐng)求多個(gè)頁(yè)面。那么file_get_contents和fopen就會(huì)出問(wèn)題。
取得的內(nèi)容也可能會(huì)不對(duì)。所以做一些類(lèi)似采集工作的時(shí)候,肯定就有問(wèn)題了。
sock較底層,配置麻煩,不易操作。 返回完整信息。
潘少寧-騰訊:
file_get_contents 雖然可以獲得某URL的內(nèi)容,但不能post get啊。
curl 則可以post和get啊。還可以獲得head信息
而socket則更底層。可以設(shè)置基于UDP或是TCP協(xié)議去交互
file_get_contents 和 curl 能干的,socket都能干。
socket能干的,curl 就不一定能干了
file_get_contents 更多的時(shí)候 只是去拉取數(shù)據(jù)。效率比較高 也比較簡(jiǎn)單。
趙的情況這個(gè)我也遇到過(guò),我通過(guò)CURL設(shè)置host 就OK了。 這和網(wǎng)絡(luò)環(huán)境有關(guān)系
首先說(shuō)下啥是socket:
php使用berkely的socket庫(kù)來(lái)創(chuàng)建他的連接,socket是一個(gè)數(shù)據(jù)結(jié)果,你可以通過(guò)這個(gè)socket來(lái)開(kāi)啟服務(wù)器和客戶(hù)端的會(huì)話。服務(wù)器端一直處于監(jiān)聽(tīng)狀態(tài),當(dāng)一個(gè)客戶(hù)端連接服務(wù)器,他就打開(kāi)服務(wù)器端正在監(jiān)聽(tīng)的一個(gè)端口進(jìn)行會(huì)話。這時(shí)服務(wù)器端接收客戶(hù)端的連接請(qǐng)求,那么就進(jìn)行一次循環(huán)。現(xiàn)在這個(gè)客戶(hù)端就能夠發(fā)送信息到服務(wù)器,服務(wù)器也可以發(fā)送信息給客戶(hù)端。
產(chǎn)生一個(gè)socket你一共需要三個(gè)變量:
1、一個(gè)協(xié)議
2、一個(gè)socket類(lèi)型
3、一個(gè)公共協(xié)議類(lèi)型
以下是對(duì)這三個(gè)變量的詳細(xì)解釋?zhuān)蟾帕私庀掳?br /> 協(xié)議:產(chǎn)生一個(gè)socket有三個(gè)協(xié)議供選擇:
1、AF_INET 這個(gè)是使用比較廣泛的產(chǎn)生socket的協(xié)議,使用tcp或者udp協(xié)議傳輸,使用ipv4地址
2、AF_INET6 顯而易見(jiàn)哈,同上,不同的是使用ipv6地址
3、AF_UNIX 使用在unix或者linux機(jī)器上,這個(gè)很少使用,僅在服務(wù)器端和客戶(hù)端均為unix或者linux系統(tǒng)上使用。
socket類(lèi)型:
1、SOCK_STREAM 這個(gè)協(xié)議是按照順序的、可靠的、數(shù)據(jù)完整的基于字節(jié)流的連接。這是一個(gè)使用最多的socket類(lèi)型,這個(gè)socket是使用TCP來(lái)進(jìn)行傳輸。
2、SOCK_DGRAM 這個(gè)協(xié)議是無(wú)連接的、固定長(zhǎng)度的傳輸調(diào)用。該協(xié)議是不可靠的,使用UDP來(lái)進(jìn)行它的連接。
3、SOCK_SEQPACKET 這個(gè)協(xié)議是雙線路的、可靠的連接,發(fā)送固定長(zhǎng)度的數(shù)據(jù)包進(jìn)行傳輸。必須把這個(gè)包完整的接受才能進(jìn)行讀取。
4、SOCK_RAW 這個(gè)socket類(lèi)型提供單一的網(wǎng)絡(luò)訪問(wèn),這個(gè)socket類(lèi)型使用ICMP公共協(xié)議。(ping、traceroute使用該協(xié)議)
5、SOCK_RDM 這個(gè)類(lèi)型是很少使用的,在大部分的操作系統(tǒng)上沒(méi)有實(shí)現(xiàn),它是提供給數(shù)據(jù)鏈路層使用,不保證數(shù)據(jù)包的順序
公共協(xié)議類(lèi)型:
1、ICMP (Internet Control Message Protocol)互聯(lián)網(wǎng)控制報(bào)文協(xié)議,主要用在網(wǎng)關(guān)和主機(jī)上,用來(lái)檢測(cè)網(wǎng)絡(luò)狀況和報(bào)告錯(cuò)誤信息
2、TCP (Transmission Control Protocol) 傳輸控制協(xié)議,他是使用最廣泛的協(xié)議,他能夠保證數(shù)據(jù)包到達(dá)接收者那里,如果中途出現(xiàn)錯(cuò)誤,那么此協(xié)議從新發(fā)送數(shù)據(jù)包。
3、UDP (User Datagram Protocol)用戶(hù)數(shù)據(jù)包協(xié)議,他是無(wú)連接的,不可靠的數(shù)據(jù)傳輸協(xié)議。
好啦,你現(xiàn)在知道了產(chǎn)生一個(gè)socke需要三個(gè)元素,那么在php中socket_create()就需要三個(gè)參數(shù),一個(gè)協(xié)議,一個(gè)socket類(lèi)型,一個(gè)公共協(xié)議。如果創(chuàng)建成功,socket_create()返回一個(gè)socket資源類(lèi)型,如果不成功,嘿嘿,那你會(huì)收到一個(gè)false.
CURL
cURL 是利用URL語(yǔ)法規(guī)定來(lái)傳輸文件和數(shù)據(jù)的工具。他支持HTTP、FTP、TELNET。
為啥要使用cURL呢?
因?yàn)椋绻覀冇袝r(shí)候想靈活的獲取網(wǎng)頁(yè)上的內(nèi)容,例如處理coockies、驗(yàn)證、表單提交、文件上傳等等等等。那么你就需要用到cURL.據(jù)說(shuō)php有著功能強(qiáng)大的cURL庫(kù)(因?yàn)榕家舱f(shuō)不清強(qiáng)大在哪里啦,
php使用cURL的選項(xiàng)基本步驟如下:
1、初始化
2、參數(shù)設(shè)置
3、頁(yè)面內(nèi)容獲取或者操作
4、釋放句柄
看看下面這個(gè)簡(jiǎn)單的例子吧。
<?php
//初始化curl
$ch = curl_init ();
/*
* 設(shè)置curl
* php手冊(cè)對(duì)于curl_setopt的解釋為:設(shè)置對(duì)于curl傳輸?shù)牟僮?nbsp;
* curl_setopt有三個(gè)參數(shù):資源(一般為你建立的curl句柄)、操作(你將對(duì)這個(gè)句柄作何操作)、參數(shù)(對(duì)于這個(gè)操作你給出的參數(shù))
*/
//例如你想對(duì)百度進(jìn)行某些操作
curl_setopt ( $ch, CURLOPT_URL, "http://www.baidu.com");
//現(xiàn)在看來(lái)你要向百度post數(shù)據(jù)
curl_setopt ( $ch, CURLOPT_POST, 1 );
/*給出了要post的數(shù)據(jù):$post_string,post的數(shù)據(jù)可以是一個(gè)文件,
*那么你需要以@加上文件的全路徑給出,或者你要post一些數(shù)據(jù),
*那么你可以按照數(shù)組形式給出,或者按照字符串給出,
*如果你想按照字符串形式給出,請(qǐng)把字符串urlencode,嘿嘿
*/
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_string );
/*
*把curl操作的結(jié)果以字符串形式 從curl_exec ()返回,而不是直接就輸出了
*/
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
//得到操作返回結(jié)果
$result = curl_exec ( $ch );
//關(guān)閉curl句柄
curl_close ( $ch );
?>
因?yàn)閜hp的curl有很多操作,要都記住估計(jì)很困難,反正偶記不住啦,說(shuō)些大家可能用的上的吧。大笑
獲取服務(wù)器的一些信息
[php] view plaincopy
<?php
//初始化curl
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, "http://www.baidu.com");
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
?>
通過(guò)上面的例子,你將會(huì)獲得如下信息:
“url” //資源網(wǎng)絡(luò)地址
“content_type” //內(nèi)容編碼
“http_code” //HTTP狀態(tài)碼
“header_size” //header的大小
“request_size” //請(qǐng)求的大小
“filetime” //文件創(chuàng)建時(shí)間
“ssl_verify_result” //SSL驗(yàn)證結(jié)果
“redirect_count” //跳轉(zhuǎn)技術(shù)
“total_time” //總耗時(shí)
“namelookup_time” //DNS查詢(xún)耗時(shí)
“connect_time” //等待連接耗時(shí)
“pretransfer_time” //傳輸前準(zhǔn)備耗時(shí)
“size_upload” //上傳數(shù)據(jù)的大小
“size_download” //下載數(shù)據(jù)的大小
“speed_download” //下載速度
“speed_upload” //上傳速度
“download_content_length”//下載內(nèi)容的長(zhǎng)度
“upload_content_length” //上傳內(nèi)容的長(zhǎng)度
“starttransfer_time” //開(kāi)始傳輸?shù)臅r(shí)間
“redirect_time”//重定向耗時(shí)
利用curl你還可以做以下操作:
模擬頁(yè)面的post操作
文件上傳
HTTP 認(rèn)證
FTP 上傳
回調(diào)函數(shù)
以上關(guān)于curl的大前提是你的php在安裝編譯時(shí)加了 --with-curlwrappers ,你可以通過(guò)phpinfo()操作來(lái)查看你是不是在編譯時(shí)加了這個(gè)擴(kuò)展庫(kù)。
如果加載了這個(gè)擴(kuò)展,你將能看見(jiàn)類(lèi)似:
如果你沒(méi)有這個(gè)擴(kuò)展,你需要改一下php.ini文件,去掉extension=php_curl.dll前面的分號(hào)。
好啦,現(xiàn)在我們知道socket和curl是咋回事了,socket是一種數(shù)據(jù)結(jié)構(gòu),他可以用來(lái)在服務(wù)器和客戶(hù)端進(jìn)行對(duì)話。而curl是利用url語(yǔ)法規(guī)定來(lái)傳輸文件和數(shù)據(jù)的規(guī)定,支持很多協(xié)議,例如FTP,HTTP、TELNET等。
抓取遠(yuǎn)程內(nèi)容,之前一直都在用file_get_content函數(shù),其實(shí)早就知道有curl這么一個(gè)好東西的存在,但是看了一眼后感覺(jué)使用頗有些復(fù)雜,沒(méi)有file_get_content那么簡(jiǎn)單,再就是需求也不大,所以沒(méi)有學(xué)習(xí)使用curl。
直到最近,要做一個(gè)網(wǎng)頁(yè)小偷程序的時(shí)候才發(fā)現(xiàn)file_get_content已經(jīng)完全不能滿足需求了。我覺(jué)得,在讀取遠(yuǎn)程內(nèi)容的時(shí)候,file_get_content除了使用比curl便捷以外,其他都沒(méi)有curl好。
php中curl和file_get_content的一些比較
主要區(qū)別:
學(xué)習(xí)才發(fā)現(xiàn),curl支持很多協(xié)議,有FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE以及LDAP,也就是說(shuō),它能做到很多file_get_content做不到的事情。curl在php可以實(shí)現(xiàn)遠(yuǎn)程獲取和采集內(nèi)容;實(shí)現(xiàn)PHP網(wǎng)頁(yè)版的FTP上傳下載;實(shí)現(xiàn)模擬登陸;實(shí)現(xiàn)接口對(duì)接(API),數(shù)據(jù)傳輸;實(shí)現(xiàn)模擬Cookie;下載文件斷點(diǎn)續(xù)傳等等,功能十分強(qiáng)大。
了解curl一些基本的使用后,才發(fā)現(xiàn)其實(shí)并不難,只不過(guò)記住里面一些設(shè)置參數(shù),難弄一點(diǎn),但是我們記住幾個(gè)常用的就可以了。
開(kāi)啟curl:
因?yàn)镻HP默認(rèn)是不支持curl功能的,因此如果要用curl的話,首先需要在php.ini中開(kāi)啟該功能,即去掉 ;extension= php_curl.dll 前面的分號(hào),然后保存后重啟apache/iis就好了。
基本語(yǔ)法:
$my_curl = curl_init(); //初始化一個(gè)curl對(duì)象
curl_setopt($my_curl, CURLOPT_URL, "http://www.jb51.net"); //設(shè)置你需要抓取的URL
curl_setopt($my_curl,CURLOPT_RETURNTRANSFER,1); //設(shè)置是將結(jié)果保存到字符串中還是輸出到屏幕上,1表示將結(jié)果保存到字符串
$str = curl_exec($curl); //執(zhí)行請(qǐng)求
echo $str; //輸出抓取的結(jié)果
curl_close($curl); //關(guān)閉url請(qǐng)求
最近需要獲取別人網(wǎng)站上的音樂(lè)數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會(huì)遇到獲取失敗的問(wèn)題,盡管按照手冊(cè)中的例子設(shè)置了超時(shí),可多數(shù)時(shí)候不會(huì)奏效:
$config['context'] = stream_context_create(array('http' => array('method' => "GET",
'timeout' => 5//這個(gè)超時(shí)時(shí)間不穩(wěn)定,經(jīng)常不奏效
)
));
這時(shí)候,看一下服務(wù)器的連接池,會(huì)發(fā)現(xiàn)一堆類(lèi)似的錯(cuò)誤,讓我頭疼萬(wàn)分:
file_get_contents(http://***): failed to open stream…
現(xiàn)在改用了curl庫(kù),寫(xiě)了一個(gè)函數(shù)替換:
function curl_file_get_contents($durl){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $durl);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
如此,除了真正的網(wǎng)絡(luò)問(wèn)題外,沒(méi)再出現(xiàn)任何問(wèn)題。
這是別人做過(guò)的關(guān)于curl和file_get_contents的測(cè)試:
file_get_contents抓取google.com需用秒數(shù):
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
curl使用的時(shí)間:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
差距很大?呵呵,從我使用的經(jīng)驗(yàn)來(lái)說(shuō),這兩個(gè)工具不只是速度有差異,穩(wěn)定性也相差很大。
建議對(duì)網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦!
方法1: 用file_get_contents 以get方式獲取內(nèi)容
<?php
$url='http://www.domain.com/';
$html = file_get_contents($url);
echo $html;
?>
方法2: 用fopen打開(kāi)url, 以get方式獲取內(nèi)容
<?php
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
?>
方法3:用file_get_contents函數(shù),以post方式獲取url
<?php
$data = array ('foo' => 'bar');
$data = http_build_query($data);
$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencodedrn" .
"Content-Length: " . strlen($data) . "rn",
'content' => $data
)
);
$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);
echo $html;
?>
方法4:用fsockopen函數(shù)打開(kāi)url,以get方式獲取完整的數(shù)據(jù),包括header和body
<?php
function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path]."?".$url[query];
echo "Query:".$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = "GET $query HTTP/1.1rn";
$request .= "Host: $url[host]rn";
$request .= "Connection: Closern";
if($cookie) $request.="Cookie: $cookien";
$request.="rn";
fwrite($fp,$request);
while()) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//獲取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,"rnrn");
$body=substr($body,4,strlen($body));
return $body;
}
return false;
}
?>
方法5:用fsockopen函數(shù)打開(kāi)url,以POST方式獲取完整的數(shù)據(jù),包括header和body
<?php
function HTTP_Post($URL,$data,$cookie, $referrer="")
{
// parsing the given URL
$URL_Info=parse_url($URL);
// Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer="111″;
// making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);
// Find out which port is needed – if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;
// building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1n";
$request.="Host: ".$URL_Info["host"]."n";
$request.="Referer: $referern";
$request.="Content-type: application/x-www-form-urlencodedn";
$request.="Content-length: ".strlen($data_string)."n";
$request.="Connection: closen";
$request.="Cookie: $cookien";
$request.="n";
$request.=$data_string."n";
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);
return $result;
}
?>
方法6:使用curl庫(kù),使用curl庫(kù)之前,可能需要查看一下php.ini是否已經(jīng)打開(kāi)了curl擴(kuò)展
<?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
php中 curl, fsockopen ,file_get_contents 三個(gè)函數(shù) 都可以實(shí)現(xiàn)采集模擬發(fā)言 。三者有什么區(qū)別,或者講究么
趙永斌:
有些時(shí)候用file_get_contents()調(diào)用外部文件,容易超時(shí)報(bào)錯(cuò)。換成curl后就可以.具體原因不清楚
curl 效率比f(wàn)ile_get_contents()和fsockopen()高一些,原因是CURL會(huì)自動(dòng)對(duì)DNS信息進(jìn)行緩存(亮點(diǎn)啊有我待親測(cè))
范佳鵬:
file_get_contents curl fsockopen
在當(dāng)前所請(qǐng)求環(huán)境下選擇性操作,沒(méi)有一概而論:
具我們公司開(kāi)發(fā)KBI應(yīng)用來(lái)看:
剛開(kāi)始采用:file_get_contents
后來(lái)采用:fsockopen
最后到至今采用:curl
(遠(yuǎn)程)我個(gè)人理解到的表述如下(不對(duì)請(qǐng)指出,不到位請(qǐng)補(bǔ)充)
file_get_contents 需要php.ini里開(kāi)啟allow_url_fopen,請(qǐng)求http時(shí),使用的是http_fopen_wrapper,不會(huì)keeplive.curl是可以的。
file_get_contents()單個(gè)執(zhí)行效率高,返回沒(méi)有頭的信息。
這個(gè)是讀取一般文件的時(shí)候并沒(méi)有什么問(wèn)題,但是在讀取遠(yuǎn)程問(wèn)題的時(shí)候就會(huì)出現(xiàn)問(wèn)題。
如果是要打一個(gè)持續(xù)連接,多次請(qǐng)求多個(gè)頁(yè)面。那么file_get_contents和fopen就會(huì)出問(wèn)題。
取得的內(nèi)容也可能會(huì)不對(duì)。所以做一些類(lèi)似采集工作的時(shí)候,肯定就有問(wèn)題了。
sock較底層,配置麻煩,不易操作。 返回完整信息。
潘少寧-騰訊:
file_get_contents 雖然可以獲得某URL的內(nèi)容,但不能post get啊。
curl 則可以post和get啊。還可以獲得head信息
而socket則更底層。可以設(shè)置基于UDP或是TCP協(xié)議去交互
file_get_contents 和 curl 能干的,socket都能干。
socket能干的,curl 就不一定能干了
file_get_contents 更多的時(shí)候 只是去拉取數(shù)據(jù)。效率比較高 也比較簡(jiǎn)單。
趙的情況這個(gè)我也遇到過(guò),我通過(guò)CURL設(shè)置host 就OK了。 這和網(wǎng)絡(luò)環(huán)境有關(guān)系
<?php
/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('jb51.net','/restServer.php',$post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30){
$socket = fsockopen($remote_server,$port,$errno,$errstr,$timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket,"POST $remote_path HTTP/1.0");
fwrite($socket,"User-Agent: Socket Example");
fwrite($socket,"HOST: $remote_server");
fwrite($socket,"Content-type: application/x-www-form-urlencoded");
fwrite($socket,"Content-length: ".strlen($post_string)+8."");
fwrite($socket,"Accept:*/*");
fwrite($socket,"");
fwrite($socket,"mypost=$post_string");
fwrite($socket,"");
$header = "";
while ($str = trim(fgets($socket,4096))) {
$header.=$str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket,4096);
}
return $data;
}
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://jb51.net/restServer.php',$post_string);
*/
function request_by_curl($remote_server,$post_string){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$remote_server);
curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://jb51.net/restServer.php',$post_string);
*/
function request_by_other($remote_server,$post_string){
$context = array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type: application/x-www-form-urlencoded'."".
'User-Agent : Jimmy's POST Example beta'."".
'Content-length: '.strlen($post_string)+8,
'content'=>'mypost='.$post_string)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server,FALSE,$stream_context);
return $data;
}
?>
/**
* 發(fā)送post請(qǐng)求
* @param string $url 請(qǐng)求地址
* @param array $post_data post鍵值對(duì)數(shù)據(jù)
* @return string
*/
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' =>; array(
'method' =>; 'POST',
'header' =>; 'Content-type:application/x-www-form-urlencoded',
'content' =>; $postdata,
'timeout' =>; 15 * 60 // 超時(shí)時(shí)間(單位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
使用如下:
post_data = array(
'username' => 'stclair2201',
'password' => 'handan'
);
send_post('http://blog.snsgou.com', $post_data);
實(shí)戰(zhàn)經(jīng)驗(yàn):
當(dāng)我利用上述代碼給另一臺(tái)服務(wù)器發(fā)送http請(qǐng)求時(shí),發(fā)現(xiàn),如果服務(wù)器處理請(qǐng)求時(shí)間過(guò)長(zhǎng),本地的PHP會(huì)中斷請(qǐng)求,即所謂的超時(shí)中斷,第一個(gè)懷疑的是PHP本身執(zhí)行時(shí)間的超過(guò)限制,但想想也不應(yīng)該,因?yàn)槔显缇桶凑者@篇文章設(shè)置了“PHP執(zhí)行時(shí)間限制”(【推薦】PHP上傳文件大小限制大全 ),仔細(xì)琢磨,想想,應(yīng)該是http請(qǐng)求本身的一個(gè)時(shí)間限制,于是乎,就想到了怎么給http請(qǐng)求時(shí)間限制搞大一點(diǎn)。。。。。。查看PHP手冊(cè),果真有個(gè)參數(shù) “ timeout ”,默認(rèn)不知道多大,當(dāng)把它的值設(shè)大一點(diǎn),問(wèn)題得已解決
Socket版本:
/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('blog.snsgou.com', '/restServer.php', $post_string);
*/
function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)");
fwrite($socket, "POST $remote_path HTTP/1.0");
fwrite($socket, "User-Agent: Socket Example");
fwrite($socket, "HOST: $remote_server");
fwrite($socket, "Content-type: application/x-www-form-urlencoded");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . "");
fwrite($socket, "Accept:*/*");
fwrite($socket, "");
fwrite($socket, "mypost=$post_string");
fwrite($socket, "");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
}
return $data;
}
Curl版本:
/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://blog.snsgou.com/restServer.php', $post_string);
*/
function request_by_curl($remote_server, $post_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "snsgou.com's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Curl版本(2)
/**
* 發(fā)送HTTP請(qǐng)求
*
* @param string $url 請(qǐng)求地址
* @param string $method 請(qǐng)求方式 GET/POST
* @param string $refererUrl 請(qǐng)求來(lái)源地址
* @param array $data 發(fā)送數(shù)據(jù)
* @param string $contentType
* @param string $timeout
* @param string $proxy
* @return boolean
*/
function send_request($url, $data, $refererUrl = '', $method = 'GET', $contentType = 'application/json', $timeout = 30, $proxy = false) {
$ch = null;
if('POST' === strtoupper($method)) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER,0 );
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($refererUrl) {
curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
}
if($contentType) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
}
if(is_string($data)){
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
}
} else if('GET' === strtoupper($method)) {
if(is_string($data)) {
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). $data;
} else {
$real_url = $url. (strpos($url, '?') === false ? '?' : ''). http_build_query($data);
}
$ch = curl_init($real_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if ($refererUrl) {
curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
}
} else {
$args = func_get_args();
return false;
}
if($proxy) {
curl_setopt($ch, CURLOPT_PROXY, $proxy);
}
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
$contents = array(
'httpInfo' => array(
'send' => $data,
'url' => $url,
'ret' => $ret,
'http' => $info,
)
);
curl_close($ch);
return $ret;
}
調(diào)用 WCF接口 的一個(gè)例子:$json = restRequest($r_url,'POST', json_encode($data));
實(shí)際上magento模版默認(rèn)就是接入了paypal的,用戶(hù)只需要到magento的后臺(tái),對(duì)Paypal帳戶(hù)進(jìn)行設(shè)置即可。
如何在magento后臺(tái)設(shè)置paypal呢? 這邊我簡(jiǎn)單跟大家分享一下。
1、System -> Configuration -> Paypal
如果沒(méi)看到Paypal,則在

2:
OFOL_DE.jpg)
OFOL_DE.jpg)

3:

后臺(tái)設(shè)定好了,保存,然后刷新緩存,就可以支付了。
注意可能會(huì)出現(xiàn)的問(wèn)題:(測(cè)試的時(shí)候很容易出現(xiàn)的問(wèn)題)
因?yàn)闇y(cè)試的時(shí)候大家一般隨便寫(xiě)地址,所以測(cè)試的時(shí)候會(huì)出現(xiàn)錯(cuò)誤:無(wú)法處理付款。由于商家提供的送貨地址無(wú)效,而且商家要求您的訂單必須送到該地址,因此,請(qǐng)與該商家進(jìn)行聯(lián)系。
解決方法:找到Paypal/Mode/Standard.php,里面有’address_override’ =1,這一行改成’address_override’=0,就可以了,大致在158行。
設(shè)置二、app/code/core/mage/paypal/model/api/standard.php 237行
$request['address_override'] = 1;將這個(gè)值設(shè)置為0 ,填寫(xiě)假的地址也可以跳轉(zhuǎn)過(guò)去了!
摘要: 1 J2me開(kāi)發(fā)網(wǎng) http://www.j2medev.com/bbs/index.asp2 J2me社區(qū) http://www.j2meforums.com/forum/3 csdn http://www.csdn.net/4 Vc知識(shí)庫(kù) http://www.vckbase.com/5 codeproject http://www.codep...
閱讀全文