2014-02-10 29 views
-2

我期待從另一個網站獲取文本併發回。例如,一個網站可能會說:PHP file_get_contents簡單示例

http://www.example.com

當前影片:符合米勒

我希望能夠把文本「會見米勒」(大電影BTW),併發布該數據我的網站。

我正在使用PHP。我很抱歉,但我是編程新手,並且在發佈之前已經搜索過,但是我無法解釋這些建議,因此我們將不勝感激。

在此先感謝。

+0

[你如何解析和處理PHP中的HTML/XML?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in -php) – PeeHaa

回答

0

要從example.com獲得想要的數據將是一個兩階段過程。首先,你可以通過curl請求獲得整個頁面。 Curl可以像Web瀏覽器一樣請求頁面,並將輸入返回給變量。其次,你可以帶上你的新變量,並隔離你的站點所需的部分。

// Start and end of string 
$start_char = '<h1>'; 
$end_char = '</h1>'; 

// Find the first occurrence of each string 
$start_pos = strpos($output, $start_char); 
$end_pos = strpos($output, $end_char); 

// Exclude the start and end parts of the string 
$start_pos += strlen($start_char); 
$end_pos += strlen($end_char); 

// Get the substring 
$string = substr($output, $start_pos, ($end_pos - $start_pos)); 

exit($string); 

聲明,這不是處理這一問題的最好辦法,一個API將是理想的,要知道,無論你的服務器和你的要求會看到這個配置更大量使用的服務器。