file_get_contents、cur、fsockopen

1 curl :需要php支持php_curl

<?php
	$url = "http://www.sina.com.cn/";
	
	$ch = curl_init($url);
	
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	
	curl_setopt($ch, CURLOPT_TIMEOUT, 60);
	
	$info = curl_exec($ch) or die("curl失败<br/>");
	
	echo strlen($info)."<br/>";


2 file_get_contents:需要支持all_url_fopen

 

<?php
$url = "http://www.sina.com.cn/";

$ctx = stream_context_create(array(
    'http' => array(
        'timeout' => 60
        )
    )
);

$info=file_get_contents($url, 0, $ctx)or die("file_get_contents超时<br/>");

 echo strlen($info)."<br/>";


3 fsockopen

<?php
	$url = "http://www.sina.com.cn/";
	
	$urlInfo = parse_url($url);
	
	$host = $urlInfo['host'];
	
	$sock = fsockopen($host, 80, $errno, $errstr, 60);
		
	if (!$sock) {
	    echo "fsockopen$errno<br />\n";
	} else {
		
		$out = "GET / HTTP/1.1\r\n";
		$out .= "Host: " . $host . "\r\n";
		$out .= "Connection: Close\r\n\r\n";

	
		fwrite($sock, $out);
	    while (!feof($sock)) {
	        $info = $info . fgets($sock, 128);
	    }
	        
	    fclose($sock);
	    echo strlen($info)."<br/>";
	    
	}

目前测试:

运行时间(s

ab -n1000 -c1000

File_get_contents

1.403

curl

1.390

fsockopen

1.270

ab -n2000 -c1000

File_get_contents

4.446

curl

4.786

fsockopen

5.185

ab -n3000 -c1000

File_get_contents

4.177

curl

4.467

fsockopen

4.649

ab -n4000 -c1000

File_get_contents

5.656

curl

5.153

fsockopen

5.324

ab -n5000 -c1000

File_get_contents

14.111

curl

11.737 

fsockopen

13.123

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值