我想从另一个网站获取文字并发回。例如,一个网站可能会说:
当前电影:遇见磨坊主
我希望能够接受“ Meet The Millers”(伟大的电影BTW)文字,并将该数据发布到我的网站上。
我正在使用PHP。抱歉,我是编程的新手,在发布之前进行了搜索,但是我无法解释这些建议,因此简单的操作将不胜感激。
提前致谢。
要从example.com获取所需的数据将需要两个阶段。首先,您可以通过curl请求获取整个页面。Curl可以像Web浏览器一样请求页面,并将输入返回到变量。
$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);
其次,您可以使用新变量并隔离站点所需的部分。
// 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是理想的选择,请注意,您的服务器和您请求的服务器在此配置中都会有更多的用途。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句