PHP file_get_contents简单示例

Cableguy

我想从另一个网站获取文字并发回。例如,一个网站可能会说:

http://www.example.com

当前电影:遇见磨坊主

我希望能够接受“ 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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP:空结果-file_get_contents

来自分类Dev

解释file_get_contents('php:// input')

来自分类Dev

PHP读取档案与file_get_contents

来自分类Dev

php file_get_contents FTP

来自分类Dev

file_get_contents返回PHP代码

来自分类Dev

PHP file_get_contents函数

来自分类Dev

在php中使用file_get_contents

来自分类Dev

PHP readfile与file_get_contents

来自分类Dev

PHP脚本返回“ file_get_contents”

来自分类Dev

PHP file_get_contents()和PHPSESSID

来自分类Dev

PHP简单HTML dom解析器,带有数组链接-file_get_contents错误

来自分类Dev

在file_get_contents('file.php')中获取变量

来自分类Dev

在functions.php中使用file_get_contents的WordPress?

来自分类Dev

file_get_contents的超时在PHP中不起作用

来自分类Dev

file_get_contents和readfile是否执行PHP代码?

来自分类Dev

Rails / Ruby等效于PHP file_get_contents

来自分类Dev

Go lang中的PHP file_get_contents

来自分类Dev

php file_get_contents()在加载图像时卡住

来自分类Dev

file_get_contents不适用于php文件

来自分类Dev

PHP-设置file_get_contents超时

来自分类Dev

file_get_contents()没有响应-php

来自分类Dev

PHP性能file_get_contents()与readfile()和cat

来自分类Dev

PHP file_get_contents转换为&amp;

来自分类Dev

输出缓冲区与PHP中的file_get_contents

来自分类Dev

使用file_get_contents()PHP函数的正确方法

来自分类Dev

php file_get_contents($ url)&变成&amp;

来自分类Dev

PHP“ file_get_contents”“无法设置验证位置”

来自分类Dev

php file_get_contents阿拉伯字符

来自分类Dev

tumblr PHP file_get_contents html未运行