不用file_get_contents,用curl时。接口调用数据获取
2016-12-30 16:04
681 查看
有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
换成curl函数的使用示例:
利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数
其实上面的这个函数还有待商榷,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误。
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
< ?php |
$file_contents = file_get_contents('http://www.ccvita.com/'); |
echo $file_contents; |
?> |
< ?php |
$ch = curl_init(); |
$timeout = 5; |
curl_setopt ($ch, CURLOPT_URL, 'http://www.ccvita.com'); |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); |
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
$file_contents = curl_exec($ch); |
curl_close($ch); |
echo $file_contents; |
?> |
< ?php |
function vita_get_url_content($url) { |
if(function_exists('file_get_contents')) { |
$file_contents = file_get_contents($url); |
} else { |
$ch = curl_init(); |
$timeout = 5; |
curl_setopt ($ch, CURLOPT_URL, $url); |
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); |
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
$file_contents = curl_exec($ch); |
curl_close($ch); |
} |
return $file_contents; |
} |
?> |
相关文章推荐
- php file_get_contents和curl请求接口并获取数据
- 使用curl、socket、file_get_contents提交数据和获取数据
- 使用curl、socket、file_get_contents提交数据和获取数据
- curl 或 file_get_contents 获取需要授权页面的方法
- 用curl 代替file_get_contents 解决获取网络资源的超时问题
- PHP利用Curl、socket、file_get_contents POST数据
- 自定义函数替代file_get_contents获取远程数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- php的file_get_contents获取不了QQ互联接口的返回值
- 解析file_get_contents模仿浏览器头(user_agent)获取数据
- php的file_get_contents获取不了QQ互联接口的返回值
- 利用Curl、socket、file_get_contents POST数据
- 提交数据的各种方式form,curl ,sockt,file_get_contents
- 使用Curl、socket、file_get_contents三种方法POST提交数据
- file_get_contents采集获取不到内容时用curl解决
- PHP下通过file_get_contents\curl的方法实现获取远程网页内容(别忘了还有PhpRPC)
- PHP利用Curl、socket、file_get_contents POST数据
- file_get_contents模仿浏览器头(user_agent)获取数据