php跨服务器访问方法小结

yipeiwu_com6年前服务器

本文实例总结了php跨服务器访问方法。分享给大家供大家参考。具体分析如下:

近来项目中遇到跨服务器访问的问题,研究了好些日子,总结如下:

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="//www.jb51.net/";
$fp=fopen($url,'r');
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo" $result";
fclose($fp);
?>

希望本文所述对大家的php程序设计有所帮助。

相关文章

使用pycharm在本地开发并实时同步到服务器

使用pycharm在本地开发并实时同步到服务器

本教程使用的是python 3   和django为事例 打开pycharm file  ->   new project...  ->...

php获取服务器信息的实现代码

复制代码 代码如下:<html><head><meta http-equiv="content-type" content="text/html; char...

python探索之BaseHTTPServer-实现Web服务器介绍

在Python探索之SocketServer详解中我们介绍了Python标准库中的SocketServer模块,了解了要实现网络通信服务,就要构建一个服务器类和请求处理类。同时,该模块还...

在Mac OS上使用mod_wsgi连接Python与Apache服务器

一、安装mod_wsgi 3.4: ./configure --with-apxs=/Users/levin/dev/apache2.2.27/bin/apxs --with-pyt...

解决django服务器重启端口被占用的问题

解决django服务器重启端口被占用的问题

在开发django项目时,启动开发服务器的命令为: python manager.py runserver [port] 其中,[port]选项指定服务器所使用的端口 根据提示,要想关...