2015-07-10 95 views
0

嗨,從PHP的網頁獲取標題標籤的內容

我想從我的網站內的頁面獲取標題標記的內容。但是,file_get_contents被禁用,所以看起來像cURL是我唯一的選擇。這就是我想:

$domain="http://example.com"; 
ob_start(); 
$curl_handle=curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, $domain. '/blog/index.php?page=4'); 
$getit = curl_exec($curl_handle); 
curl_close($curl_handle); 
ob_end_clean(); 
preg_match("/<title>(.*)<\/title>/i", $getit, $matches); 
$title= $matches[1]; 

我不得不使用ob_start和清潔,因爲否則稱爲頁面嵌入到我最後的HMTL代碼,我不需要。我只需要獲取標籤值並讓$ title顯示它,但它什麼都不顯示。這裏有什麼問題?

謝謝。

+0

做嘗試打印$ GETIT?或$匹配?並看看結果是什麼 –

+1

是的,getit的結果是「1」,但$匹配只能打印「數組」。 –

+0

完整的HTML你能看到我的標題? –

回答

1

使用

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); 

最後的代碼應該是

$domain="http://example.com"; 
ob_start(); 
$curl_handle=curl_init(); 
curl_setopt($curl_handle, CURLOPT_URL, $domain. '/blog/index.php?page=4'); 
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true); 
$getit = curl_exec($curl_handle); 
curl_close($curl_handle); 
ob_end_clean(); 
preg_match("/<title>(.*)<\/title>/i", $getit, $matches); 
$title= $matches[1]; 
+0

謝謝,我錯過了那一行。但是,似乎ob_start()也不是必需的。還有一個問題,被調用頁面的編碼與實際頁面不同。是否有可能改變編碼? –

+0

沒關係,我明白了!謝謝。 –

+0

嘿嘗試使用curl_setopt($ curl_handle,CURLOPT_ENCODING,「」); –