首页 > 开发 > Php > 正文

php怎么调用远程数据?实现代码

2020-09-12 12:52:18
字体:
来源:转载
供稿:网友
本文介绍了php调用远程数据的方法,php curl调用远程数据的例子,需要的朋友参考下。

默认不打开文件系统和流配置选项 allow_url_fopen ,建议使用一个替代的函数模块 cURL。
php cURL 远程读取数据的方法:

例1,allow_url_fopen = On
 

代码示例:
$str = file_get_contents("https://www.mycodes.net");
if ($str !== false) {
// do something with the content
echo $str;
}
?>

例2,allow_url_fopen = Off
 

代码示例:
$ch = curl_init("https://www.mycodes.net/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($ch);
if ($str !== false) {
// do something with the content
echo $str;
}
curl_close($ch);
?>

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表