2016-04-14 108 views
0

我收到成千上萬的錯誤日誌,看起來像這樣:FEOF()預計參數1是資源

$xml = "<requisicao-boleto> 
       <website> 
        <n_website>{$n_website}</n_website> 
        <password>{$password}</password> 
       </website> 

       <sacado> 
        <name>{$name}</name> 
        {$user_code} 
        <address> 
         <street>{$street}</street> 
         <complement>{$complement}</complement> 
         <number>{$number}</number> 
         <district>{$district}</district> 
         <state_province>{$state_province}</state_province> 
         <city>{$city}</city> 
         <postcode>{$postcode}</postcode> 
         <country>{$country}</country> 
        </address> 
       </sacado> 
       <dados_boleto> 
        <product>{$product}</product> 
        <reference>{$uid}</reference> 
        <value>{$value}</value> 
       </dados_boleto> 
      </requisicao-boleto>"; 

    $xml = preg_replace('/\s(?=\s)/', '', $xml); 
    $xml = "xml=" . $xml; 

    $n = strlen($xml); 

    $opts = array(
     'http' => array(
      'method' => "POST", 
      'header' => "User-Agent: My Own Http Client\r\n" . 
       "Content-length: " . $n . "\r\n", 
      'content' => $xml 
     ) 
    ); 

    $context = stream_context_create($opts); 

    $handle = fopen($URL, 'r', false, $context); 

    $conteudo = ''; 

    while (!feof($handle)) { 
     $conteudo .= fread($handle, 1024); 
    } 

的代碼是:

while (!feof($handle)) { 
    $conteudo .= fread($handle, 1024); 
} 

已經通過呈三角問題,任何人,知道如何克服這個問題?

我已經用完整的代碼更新了這個問題,同時我嘗試執行已經給出的th消耗。

+0

$ handel有什麼? –

+0

$ handle = fopen($ URL,'r',false,$ context); @ChetanAmeta – danrodrigues

+2

所以你的fopen()失敗了 –

回答

1

你檢查過fopen是否有效?

$handle = fopen($URL, 'r', false, $context); 
if ($handle === FALSE) { 
    echo 'Cannot open this url ' . $URL; 
    exit; 
} 
1

檢查fopen

文檔成功返回文件指針資源,或錯誤FALSE。

我覺得你的情況fopen返回falsefeof也返回false。這就是你無限循環的原因。

下面是feof文檔報價

返回TRUE,如果文件指針是在EOF或發生錯誤(包括套接字超時);否則返回FALSE。

你舉的例子是這樣的

php > var_dump(feof(false)); 
PHP Warning: feof() expects parameter 1 to be resource, boolean given in php shell code on line 1 
PHP Stack trace: 
PHP 1. {main}() php shell code:0 
PHP 2. feof() php shell code:1 
bool(false) 
php > 

您需要檢查是否$handle是在做任何事情之前的資源。

相關問題