2013-06-25 77 views
3

我試圖閱讀通訊社的rss站點,並獲得所有新聞的幾個選項保存在我的數據庫。所以我使用php函數作爲file_get_contents或cURl,但它需要大約一分鐘的時間來獲取網站的內容,並分析它分離我想要的新聞部分。PHP的file_get_contents是緩慢的,並返回500內部服務器錯誤

這是我的代碼的一部分,我得到的消息datails從RSS:

$rss = new DOMDocument(); 
$rss->load('http://isna.ir/fa/Sports/feed'); 
$feed = array(); 
foreach ($rss->getElementsByTagName('item') as $node) { 
    $item = array ( 
     'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 
     'category' => $node->getElementsByTagName('category')->item(0)->nodeValue, 
     'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 
     'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, 
     ); 
    array_push($feed, $item); 
} 
$title = str_replace(' & ', ' & ', $feed[0]['title']); 
    $link = $feed[0]['link']; 
    $category = $feed[0]['category']; 
    $date = date('l F d, Y', strtotime($feed[0]['date'])); 

而在這部分我用的新聞鏈接,以獲得從原來的新聞頁面中的照片:

$context = stream_context_create(array('http' => array('header'=>'Connection: close'))); 

$f = explode("news", $link); 
$photo_link = $f[0]. 'photo' .$f[1]; 

$ff = file_get_contents($photo_link, false, $context); 
$f1 = explode('<div class="news-image">', $ff); 
$f2 = explode('<h1', $f1[1]); 
$f3 = explode('href="', $f2[0]); 
$f4 = explode('">', $f3[1]); 
$image = $f4[0]; 

echo '<img src="' .$image. '"></img>'; 

這是大多數時候的結果:

Warning: file_get_contents(http://isna.ir/fa/photo/92040301515/مدافع-تیم-ملی-آلمان-از-فوتبال-خداحافظی-کرد) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /opt/lampp/htdocs/example8/reader.php 

我用捲曲的功能太多,但沒有太多獲得更好的結果!編碼URL的阿拉伯語部分

+0

您是否嘗試過URL編碼的圖像網頁請求網址? – tlenss

+0

如果你有'500內部服務器錯誤',這意味着該文件不可讀 – bystwn22

+1

@ bystwn22該URL工作正常!你可以在你評論之前測試它! – tlenss

回答

2

試試這個

<?php 
    $photo_link = explode("news", $link); 

    $first = $photo_link[0]; 
    $last = str_replace(basename($photo_link[1]), urlencode(basename($photo_link[1])), $photo_link[1]); 

    $photo_link = $first."news".$last; 
    print_r(file_get_contents($photo_link, false, $context)); 
?> 

讓你完整的代碼會是這樣的

<?php 
    $feed = array(); 
    $rss = new DOMDocument(); 
    $rss->load('http://isna.ir/fa/Sports/feed'); 

    foreach($rss->getElementsByTagName('item') as $node) { 
    $feed[] = array(
     'title'  => str_replace(" & ", " &amp; ", $node->getElementsByTagName('title')->item(0)->nodeValue), 
     'category' => $node->getElementsByTagName('category')->item(0)->nodeValue, 
     'link'  => $node->getElementsByTagName('link')->item(0)->nodeValue, 
     'date'  => strtotime($node->getElementsByTagName('pubDate')->item(0)->nodeValue) 
    ); 
    } 

    $title = $feed[0]["title"]; 
    $link  = $feed[0]["link"]; 
    $category = $feed[0]["category"]; 
    $date  = date("l F d, Y", $feed[0]["date"]); 

    print_r($feed); 

    $context = stream_context_create(
    array(
     'http' => array(
     'header' => 'Connection: close' 
    ) 
    ) 
); 

    $f = explode("news", $link); 

    /** My Code Starts **/ 
    $f[1] = str_replace(basename($f[1]), urlencode(basename($f[1])), $f[1]); 
    /** My Code Ends **/ 

    $photo_link = $f[0]."photo".$f[1]; 

    $ff = file_get_contents($photo_link, false, $context); 
    $f1 = explode('<div class="news-image">', $ff); 
    $f2 = explode('<h1', $f1[1]); 
    $f3 = explode('href="', $f2[0]); 
    $f4 = explode('">', $f3[1]); 
    $image = $f4[0]; 

    echo '<img src="'.$image.'"></img>'; 
?> 
2

嘗試URL

$urlParts = explode('/', $f[1]); 
foreach ($urlParts as $idx => $urlPart) { 
    $urlParts[$idx] = urlencode($urlPart); 
} 

$photo_link = $f[0]. 'photo' . implode('/', $urlParts); 
var_dump(file_get_contents($photo_link)); 
+0

由於$ f [1]有兩個部分,其中一個是阿拉伯語,現在這是$ f1 [1]的第一部分的問題,它的格式類似於**/number/**。 –

+0

**/**使用urlencode()函數更改爲**%2F **! –

+0

嘗試將字符串分解爲/並對最後一部分進行編碼 – tlenss

相關問題