PHP远程读取问题? 外部网站上有一个页面,在本站用php卖取这个页面,获得内容并返回给客户端。file_get_contents、curl、fsockopen都不能用,被服务器屏蔽了,还有什么可以使用的方法吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1function urlfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE , $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') {02 $return = '';03 $matches = parse_url($url);04 $host = $matches['host'];05 $path = $matches['path'] ? $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : '') : '/';06 $port = !empty($matches['port']) ? $matches['port'] : 80;07 08 if($post) {09 $out = "POST $path HTTP/1.0\r\n";10 $out .= "Accept: */*\r\n";11 $out .= "Accept-Language: zh-cn\r\n";12 $boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n")));13 $out .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";14 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";15 $out .= "Host: $host\r\n";16 $out .= 'Content-Length: '.strlen($post)."\r\n";17 $out .= "Connection: Close\r\n";18 $out .= "Cache-Control: no-cache\r\n";19 $out .= "Cookie: $cookie\r\n\r\n";20 $out .= $post;21 } else {22 $out = "GET $path HTTP/1.0\r\n";23 $out .= "Accept: */*\r\n";24 $out .= "Accept-Language: zh-cn\r\n";25 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";26 $out .= "Host: $host\r\n";27 $out .= "Referer: \r\n";28 $out .= "Connection: Close\r\n";29 $out .= "Cookie: $cookie\r\n\r\n";30 }31 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);32 if(!$fp) {33 return '';34 } else {35 stream_set_blocking($fp, $block);36 stream_set_timeout($fp, $timeout);37 @fwrite($fp, $out);38 $status = stream_get_meta_data($fp);39 if(!$status['timed_out']) {40 while (!feof($fp)) {41 if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {42 break;43 }44 }45 46 $stop = false;47 while(!feof($fp) && !$stop) {48 $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));49 $return .= $data;50 if($limit) {51 $limit -= strlen($data);52 $stop = $limit <= 0;53 }54 }55 }56 @fclose($fp);57 return $return;58 }59 } file_get_contents()比file()慢?如果抓取别人的页面还是用file_get_contents()比较好~file()取回的是数组而file_get_contents()取回的是字符串你是想取回个数组再把它们连起来方便?还是直接把他们取回来方便?另外file_get_contents(String,int)有个可选参数设定读取的长度在PHP手册中说:file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。 一步步测试,现在发现问题出在要读取的服务器上,用浏览器可以打开,但用PHP程序远程调用却不能返回,使用fsockopen获得的错误信息是:由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。(10060)。我在本机上测试可以打开,本机的环境是WINDOWS+IIS+PHP,但原样的程序上传到服务器就出错了,谁有出过这样的问题吗?求解..... 网址 http://www.tjyute.com/dom.php 用浏览器访问 http://www.tjyute.com/dom.php得到 curl<br />用php访问echo file_get_contents('http://www.tjyute.com/dom.php');得到 curl<br />等待时间都在10秒以上 恩,我定义的访问超时是10秒,curl是说现在启用的是curl方式,看来是没办法解决了,不行只能租用独立 服务器了,主要是这点访问量专门租个服务器也太不值了,100块可以解决的事,一定要几千块来做,好无语啊 用php动态生成EXCEL,打开文件时提示“工作表设置错误” 有没有高效的下拉导航菜单实例 请php高手帮个忙,怎么远程调用参数? mysql的排序问题 熟悉blog开发的大虾请进! 请问我的system函数怎么不能用了? 浏览器的默认语言总是西欧字符,怎样改换? 一个小问题 phpmyadmin传入空间问题 请教$_SESSION[]的具体用法 在php同一页面怎么把文本框的值赋给php的变量呢? 求高手,求围观啊,急啊
function urlfopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE , $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE') {
02
$return = '';
03
$matches = parse_url($url);
04
$host = $matches['host'];
05
$path = $matches['path'] ? $matches['path'].(isset($matches['query']) ? '?'.$matches['query'] : '') : '/';
06
$port = !empty($matches['port']) ? $matches['port'] : 80;
07
08
if($post) {
09
$out = "POST $path HTTP/1.0\r\n";
10
$out .= "Accept: */*\r\n";
11
$out .= "Accept-Language: zh-cn\r\n";
12
$boundary = $encodetype == 'URLENCODE' ? '' : ';'.substr($post, 0, trim(strpos($post, "\n")));
13
$out .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data$boundary\r\n";
14
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
15
$out .= "Host: $host\r\n";
16
$out .= 'Content-Length: '.strlen($post)."\r\n";
17
$out .= "Connection: Close\r\n";
18
$out .= "Cache-Control: no-cache\r\n";
19
$out .= "Cookie: $cookie\r\n\r\n";
20
$out .= $post;
21
} else {
22
$out = "GET $path HTTP/1.0\r\n";
23
$out .= "Accept: */*\r\n";
24
$out .= "Accept-Language: zh-cn\r\n";
25
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
26
$out .= "Host: $host\r\n";
27
$out .= "Referer: \r\n";
28
$out .= "Connection: Close\r\n";
29
$out .= "Cookie: $cookie\r\n\r\n";
30
}
31
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
32
if(!$fp) {
33
return '';
34
} else {
35
stream_set_blocking($fp, $block);
36
stream_set_timeout($fp, $timeout);
37
@fwrite($fp, $out);
38
$status = stream_get_meta_data($fp);
39
if(!$status['timed_out']) {
40
while (!feof($fp)) {
41
if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
42
break;
43
}
44
}
45
46
$stop = false;
47
while(!feof($fp) && !$stop) {
48
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
49
$return .= $data;
50
if($limit) {
51
$limit -= strlen($data);
52
$stop = $limit <= 0;
53
}
54
}
55
}
56
@fclose($fp);
57
return $return;
58
}
59
}
file()取回的是数组
而file_get_contents()取回的是字符串你是想取回个数组再把它们连起来方便?还是直接把他们取回来方便?另外
file_get_contents(String,int)
有个可选参数设定读取的长度在PHP手册中说:
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法。如果操作系统支持还会使用内存映射技术来增强性能。
得到 curl<br />用php访问
echo file_get_contents('http://www.tjyute.com/dom.php');
得到 curl<br />等待时间都在10秒以上