trim($file)显示未知错误


(trim($file) shows unknown error

我试着编写一个小的"从页面X抓取信息" -脚本。我从互联网上得到一个代码,但它不工作:

警告:trim()期望参数1为字符串,资源在

中给出
$file = @fopen ($url,"r");
if (trim($file) == "")   <<<<errormessage belongs to here. 

所以我试着检查$file是否不是字符串。PHP说:
不是字符串
不是整型
不是数字
不是数组

那么它是什么呢?苹果派吗?: D

如何修复这个脚本使用它?

整个脚本如下:

<?php
// URL, die durchsucht werden soll
$url = "http://xxxxxxxx/community/accounts/xxxxxxxxxxxx/";
// Zeichenfolge vor relevanten Einträgen
$startstring .= "<td class='"td-minor'">";
$startstring1 .= "<td class='"td-value'">";
// bis zum nächsten html tag bzw. Zeichenfolge nach relevanten Einträgen
$endstring = "</td>"; 
$file = @fopen ($url,"r");
if (trim($file) == "")
 {
  echo "Service out of order";
 } 
 else 
 {
  $i=0;
    while (!feof($file)) 
    {
     // Wenn das File entsprechend groß ist, kann es unter Umständen
     // notwendig sein, die Zahl 2000 entsprechend zu erhöhen. Im Falle
     // eines Buffer-Overflows gibt PHP eine entsprechende Fehlermeldung aus.
     $zeile[$i] = fgets($file,2000);
     $i++;
    }
  fclose($file);
 }
// Nun werden die Daten entsprechend gefiltert.
for ($j=0;$j<$i;$j++) 
{
 if ($resa = strstr($zeile[$j],$startstring)) 
 {
  $resb = str_replace($startstring, "", $resa);
  $endstueck = strstr($resb, $endstring);
  $resultat .= str_replace($endstueck,"",$resb);
  $resultat .= ";";
 }
}
for ($k=0;$k<$i;$k++) 
{
 if ($resa = strstr($zeile[$k],$startstring1)) 
 {
  $resb = str_replace($startstring1, "", $resa);
  $endstueck = strstr($resb, $endstring);
  $resultat1 .= str_replace($endstueck,"",$resb);
  $resultat1 .= ";";
 }
}
// Ausgabe der Daten
$array_0 = explode(";",$resultat);
$array_1 = explode(";",$resultat1);
for ($i = 0; $i <= count($array_0); $i++ ) 
 {
  echo "".$array_0[$i].$array_1[$i]."<br>";
 }
return $resultat;
?>

我只是

$file = @fopen ($url,"r");

一个URL .

如警告所示,它是一个资源。根据fopen的手册,http://php.net/manual/en/function.fopen.php

成功时返回文件指针资源,错误时返回FALSE。

您可以使用,fread, http://php.net/manual/en/function.fread.php。它将读取资源。

您也可以使用file_get_contents来代替fopenfread配对。file_get_contents默认返回字符串。

所以你的代码应该是…

$file = file_get_contents($url);

也尽量避免使用关闭有用消息的@

fopen()函数返回一个资源,正如错误消息所述。如果您只需要内容,您可以使用file_get_contents():

$data = file_get_contents($url);
if (trim($data) === "") {
    // stuff
}