时间:2021-07-01 10:21:17 帮助过:26人阅读
具体分析如下:
近来项目中遇到跨服务器访问的问题,研究了好些日子,总结如下:
1、用file_get_contents方法
$host = 'url'; $randomNumber=file_get_contents($host); echo $$randomNumber;
2、用Curl
$host = 'url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
// 返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// 使用POST提交
curl_setopt($ch, CURLOPT_POST, 1);
// POST参数
$str = array('a=1','b=2','c=3');
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
// 结果
$res = curl_exec($ch);
curl_close($ch);使用curl库,使用curl库之前,你可能需要查看一下php.ini,查看是否已经打开了curl扩展
3、 用fopen打开url, 以get方式获取内容
<?php
$url="http://www.gxlcms.com/";
$fp=fopen($url,'r');
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo" $result";
fclose($fp);
?>相关推荐:
js中跨域的方法
laravel开发中关于跨域的解决方法详解
AJAX原理与CORS跨域的方法
以上就是php实现跨服务器访问的详细内容,更多请关注Gxl网其它相关文章!