问题描述:

I am looking to grab text from another website and post back. For example a website might say:

http://www.example.com

Current Movie: Meet The Millers

I want to be able to take the text "Meet The Millers" (Great movie BTW) and post that data to my website.

I am using PHP. I am sorry, but I am new to programming and I have searched prior to posting, however I cannot interpret the suggestions so something simple would be greatly appreciated.

Thanks in advance.

网友答案:

To get the data you want from example.com would be a two stage process. Firstly, you could get the whole page via curl request. Curl can request pages just like a web browser, and return the input to a variable.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output is the requested page's source code
$output = curl_exec($ch);
curl_close($ch);

Secondly, you could take your new variable and isolate the part that you want for your site.

// 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);

Disclaimer, this isn't the best way to approach the issue, an API would be ideal, be aware that both your server and the server you're requesting will see a lot more usage in this configuration.

相关阅读:
Top