急:有关fsockopen()的问题 按道理说,依旧我这样写法,就算打不开指定的文件,也不会出现php报错信息啊.这叫我怎么用fsockopen()啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 $url="blog.donews.com"; //或者 $url="61.135.128.148";$fp = fsockopen($url,80, &$errno, &$errstr, 10); 打开手册好好看。你的用法是fopen的。 这里有一段函数,模拟http请求的,不知你是否用得上print_r(HTTPrequest('get', 'www.w3.org', '/'));function HTTPrequest ($method, $host, $usepath, $postdata = "" ){ if (is_array($postdata)) { foreach ($postdata as $key=>$val) { if(!is_integer($key)) $data .= "&$key=".urlencode($val); } $data = substr($data, 1); } else { $data = $postdata; } $fp = fsockopen($host, 80, &$errno, &$errstr, 30); if (!$fp) { print "$errstr ($errno)<br>\n"; return false; } else { if (strtoupper($method) == "GET") { $headers = "GET $usepath HTTP/1.1\r\n"; } else if (strtoupper($method) == "POST") { $headers = "POST $usepath HTTP/1.1\r\n"; } $headers .= "Host: $host\n"; $headers .= "Connection: close\r\n"; //$headers .= "Accept-Encoding: gzip\r\n"; if (strtoupper($method) == "POST") { $strlength = strlen($data); $headers .= "Content-Type: application/x-www-form-urlencoded\r\n"; $headers .= "Content-Length: ".$strlength."\r\n"; } $headers .= "\r\n"; $headers .= "$data\r\n"; fputs($fp, $headers); while (!feof( $fp )) { $output[] = fgets($fp, 1024); } fclose( $fp); return $output; }}可以自己加一些诸如refer,agent一类的http头进去 第一个错误信息是说找不到目标地址第二个错误信息是说连接失败(不到目标地址,当然连接失败)目标网站没有工作、dns服务器故障、端口不对都可能造成请先用浏览器确认目标正确注意:如果你需要使用代理服务器上网的话,请停止这个徒劳的测试 我的应该没有错啊完全代码在这,高手帮我看看function my_rss($url,$num="") { global $glo_title; global $glo_link; global $glo_description; global $glo_date; $rdf = parse_url($url); $fp = fsockopen($rdf['host'], 80, &$errno, &$errstr, 10); if (!$fp) { $content = "error"; echo $errno.":".$errstr."<br>"; return; } if ($fp) { fputs($fp, "GET " . $rdf['path'] . "?" . $rdf['query'] . " HTTP/1.0\r\n"); fputs($fp, "HOST: " . $rdf['host'] . "\r\n\r\n"); $string = ""; while(!feof($fp)) { $pagetext = fgets($fp,300); $string .= chop($pagetext); } fputs($fp,"Connection: close\r\n\r\n"); fclose($fp); $items = explode("</item>",$string); $content = "<font class=\"content\">"; if ($num==""){ $num=count($items)-1; } for ($i=0;$i<$num;$i++) { $link = ereg_replace(".*<link>","",$items[$i]); $link = ereg_replace("</link>.*","",$link); $title2 = ereg_replace(".*<title>","",$items[$i]); $title2 = ereg_replace("</title>.*","",$title2); $description = ereg_replace(".*<description>","",$items[$i]); $description = ereg_replace("</description>.*","",$description); $thedate = ereg_replace(".*<pubDate>","",$items[$i]); $thedate = ereg_replace("</pubDate>.*","",$thedate); $glo_title[$i]=strip_tags(iconv('utf-8','gb2312' ,$title2)); $glo_link[$i]=iconv('utf-8','gb2312' ,$link); $glo_description[$i]=strip_tags(iconv('utf-8','gb2312' ,$description)); $glo_date[$i]=strip_tags(iconv('utf-8','gb2312' ,$thedate)); } } } xuzuning(唠叨) ,我现在是在本地局域网中测试的,服务器通过网关可以上网,等网站做好后就会直接放在web上,在局域网用此函数为出错?我运行此段代码时好时坏,被请救的地址是没有问题的. 我还有一个从fsockopen()取出字符,但"<p>"总会转成"<",请问如何才能把它转会来.我开个新贴,希望高手指点一下 问问大家函数命名规则? php mysql 字符转义问题 js改写成jquery glob 函数实现原理 如何将会话内容反序列化成数组 页面跳转问题 如何在win2000下配置PHP网站+mysql数据库? 文章发表,冲星,开心,散分, 明天上班了,今天找找感觉,全忘了,哭啊! MiMe类型有几种怎样写? 高分求救!过来看看!关于刷新的问题 如何获取当前页面中的表单数据呀?
$fp = fsockopen($url,80, &$errno, &$errstr, 10);
你的用法是fopen的。
{
if (is_array($postdata)) {
foreach ($postdata as $key=>$val) {
if(!is_integer($key))
$data .= "&$key=".urlencode($val);
}
$data = substr($data, 1);
} else {
$data = $postdata;
} $fp = fsockopen($host, 80, &$errno, &$errstr, 30); if (!$fp) {
print "$errstr ($errno)<br>\n";
return false;
} else {
if (strtoupper($method) == "GET") {
$headers = "GET $usepath HTTP/1.1\r\n";
} else if (strtoupper($method) == "POST") {
$headers = "POST $usepath HTTP/1.1\r\n";
}
$headers .= "Host: $host\n";
$headers .= "Connection: close\r\n";
//$headers .= "Accept-Encoding: gzip\r\n";
if (strtoupper($method) == "POST") {
$strlength = strlen($data);
$headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
$headers .= "Content-Length: ".$strlength."\r\n";
}
$headers .= "\r\n";
$headers .= "$data\r\n";
fputs($fp, $headers);
while (!feof( $fp )) {
$output[] = fgets($fp, 1024);
}
fclose( $fp);
return $output;
}
}可以自己加一些诸如refer,agent一类的http头进去
第二个错误信息是说连接失败(不到目标地址,当然连接失败)目标网站没有工作、dns服务器故障、端口不对都可能造成
请先用浏览器确认目标正确注意:如果你需要使用代理服务器上网的话,请停止这个徒劳的测试
完全代码在这,高手帮我看看
function my_rss($url,$num="") {
global $glo_title;
global $glo_link;
global $glo_description;
global $glo_date;
$rdf = parse_url($url);
$fp = fsockopen($rdf['host'], 80, &$errno, &$errstr, 10);
if (!$fp) {
$content = "error";
echo $errno.":".$errstr."<br>";
return;
}
if ($fp) {
fputs($fp, "GET " . $rdf['path'] . "?" . $rdf['query'] . " HTTP/1.0\r\n");
fputs($fp, "HOST: " . $rdf['host'] . "\r\n\r\n");
$string = "";
while(!feof($fp)) {
$pagetext = fgets($fp,300);
$string .= chop($pagetext);
}
fputs($fp,"Connection: close\r\n\r\n");
fclose($fp);
$items = explode("</item>",$string);
$content = "<font class=\"content\">";
if ($num==""){
$num=count($items)-1;
}
for ($i=0;$i<$num;$i++) {
$link = ereg_replace(".*<link>","",$items[$i]);
$link = ereg_replace("</link>.*","",$link);
$title2 = ereg_replace(".*<title>","",$items[$i]);
$title2 = ereg_replace("</title>.*","",$title2);
$description = ereg_replace(".*<description>","",$items[$i]);
$description = ereg_replace("</description>.*","",$description);
$thedate = ereg_replace(".*<pubDate>","",$items[$i]);
$thedate = ereg_replace("</pubDate>.*","",$thedate);
$glo_title[$i]=strip_tags(iconv('utf-8','gb2312' ,$title2));
$glo_link[$i]=iconv('utf-8','gb2312' ,$link);
$glo_description[$i]=strip_tags(iconv('utf-8','gb2312' ,$description));
$glo_date[$i]=strip_tags(iconv('utf-8','gb2312' ,$thedate));
}
}
}
我现在是在本地局域网中测试的,服务器通过网关可以上网,等网站做好后就会直接放在web上,在局域网用此函数为出错?我运行此段代码时好时坏,被请救的地址是没有问题的.