资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php获取远程接口数据,php获取远程接口数据的方法

php连接远程数据库

在php中如果要连接远程数据库连接方法很简单,只要把本地连接localhost或127.0.0.1改成指定远程服务器一IP地址或者直接域名即可。

10年积累的成都网站制作、成都网站设计、外贸营销网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先建设网站后付款的网站建设流程,更有纳溪免费网站建设让你可以放心的选择与我们合作。

语法

mysql_connect(servername,username,password);

例子

在下面的例子中,我们在一个变量中 ($con) 存放了在脚本中供稍后使用的连接。如果连接失败,将执行 "die" 部分:

代码如下:

?php

$con = mysql_connect("localhost","peter","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

// some code

?

上面是连接本地数据库,下面把localhost改成远程IP即可了

实例 代码如下:

$conn=mysql_connect('','root','123456888');

if(!$conn) echo "失败!";

else echo "成功!";

// 从表中提取信息的sql语句

$sql="SELECT * FROM user where userName='$user_name'";

// 执行sql查询

$result=mysql_db_query('info', $sql, $conn);

// 获取查询结果

$row=mysql_fetch_row($result);

mysql_close();

php怎么获取远程JSon内容 并post一些参数

$data = file_get_contents($url);//目的页面内容获取

$t = json_decode($data,1);//转换为PHP数组

//处理...

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $urlo);//数据发送地址

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//发送的数据数组

curl_exec($ch);

php中有哪些常用的远程请求发送方法

1、用file_get_contents 以get方式获取内容:

?php

$url = '' ;

$html = file_get_contents ( $url );

echo $html ;

?

2、用fopen打开url,用get方式获取

$fp = fopen ( $url , 'r' );

stream_get_meta_data( $fp );

while (! feof ( $fp )) {

$result .= fgets ( $fp , 1024);

}

echo "url body: $result" ;

fclose( $fp );

3、用file_get_contents 以post方式获取内容:

$data = array ( 'foo' = 'bar' );

$data = http_build_query($data);

$opts = array (

'http' = array (

'method' = 'POST' ,

'header' = "Content-type: application/x-www-form-urlencodedrn" . 'Content-Length: ' . strlen($data) . 'rn' , 'content' = $data ) ); $context = stream_context_create($opts); $html = file_get_contents( '' , false , $context); echo $html;

4、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启

function get_url ( $url , $cookie =false)

{

$url = parse_url ( $url );

$query = $url [path]. '?' . $url [query];

echo 'Query:' . $query ;

$fp = fsockopen ( $url [host], $url [port]? $url [port]:80 , $errno , $errstr , 30);

if (! $fp ) {

return false;

} else {

$request = 'GET $query HTTP/1.1rn' ;

$request .= 'Host: $url[host]rn' ;

$request .= 'Connection: Closern' ;

if ( $cookie ) $request .= 'Cookie: $cookien' ;

$request .= 'rn' ;

fwrite( $fp , $request );

while (!@ feof ( $fp )) {

$result .= @ fgets ( $fp , 1024);

}

fclose( $fp );

return $result ;

}

}

//获取url的html部分,去掉header

function GetUrlHTML( $url , $cookie =false)

{

$rowdata = get_url( $url , $cookie );

if ( $rowdata )

{

$body = stristr ( $rowdata , 'rnrn' );

$body = substr ( $body ,4, strlen ( $body ));

return $body ;

}

return false;

}

5、 用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

function HTTP_Post( $URL , $data , $cookie , $referrer = '' )

{

// parsing the given URL

$URL_Info = parse_url ( $URL );

// Building referrer

if ( $referrer == '' ) // if not given use this script as referrer

$referrer = '111' ;

// making string from $data

foreach ( $data as $key = $value )

$values []= '$key=' .urlencode( $value );

$data_string =implode( '' , $values );

// Find out which port is needed – if not given use standard (=80)

if (!isset( $URL_Info [ 'port' ]))

$URL_Info [ 'port' ]=80;

// building POST-request:

$request .= "POST " . $URL_Info [ 'path' ]. " HTTP/1.1n" ; $request .= "Host: " . $URL_Info [ 'host' ]. "n" ; $request .= "Referer: $referern" ; $request .= "Content-type: application/x-www-form-urlencodedn" ; $request .= 'Content-length: ' . strlen ( $data_string ). "n" ; $request .= 'Connection: closen' ; $request .= 'Cookie: $cookien' ; $request .= 'n' ; $request .= $data_string . 'n' ; $fp = fsockopen ( $URL_Info [ 'host' ], $URL_Info [ 'port' ]); fputs ( $fp , $request ); while (! feof ( $fp )) { $result .= fgets ( $fp , 1024); } fclose( $fp ); return $result ;

}

6、 使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

$ch = curl_init();

$timeout = 5;

curl_setopt ( $ch , CURLOPT_URL, ‘http: //');

curl_setopt ( $ch , CURLOPT_RETURNTRANSFER, 1);

curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT, $timeout );

$file_contents = curl_exec( $ch );

curl_close( $ch );

echo $file_contents ;

以上就是php中,比较常用的6中远程请求方法,希望对php新人的学习、工作有一定的帮助。当然远程请求的方法肯定不止题主上面为大家介绍的这6中,如果你还有更好的方法,欢迎补充分享。软件开发的学习,就是一个分享式的学习,让我们一起在分享学习中,共进步。

怎么用php获取远程端的json数据,不会用,求大神解释

file_get_contents是可以的,

?php

echo "meta http-equiv='Content-Type' content='text/html; charset=utf-8' /";

$m = file_get_contents(";client_id=319cdac7553fa298");

print_r(json_decode($m));

?

输出结果:

PHP CURL 获取远程数据下载

这样做肯定是用的你的带宽,是把文件下载到你的服务器上,然后再下载给客户端。

有两条路你可以去试试看,我没做过:一是setcookie指定域名是那个网站,然后转向:

setcookie ($cname ,$cvalue ,$expire ,$path , $host);

header('location: $url");

另外一个方法类似,好像有个P3P可以传递COOKIE,需要你自己查资料:

setcookie ($cname ,$cvalue);

header('P3P: ....');

header('location: $url");

第二个办法应该是可以的,陶宝和开心网都在用这样的技术,陶宝有许多域名,一次登录后都可以使用,就是利用P3P实现的COOKIE传递。

如何用php调用外部接口json数据

两种比较简单的方法:

1、使用curl

$url = "";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_TIMEOUT , 30);

$output = curl_exec($ch);

curl_close($ch);

echo $output;

2、使用file_get_contents

$output = file_get_contents($url);

echo $output;

3 、使用socket 也是可以的


网站名称:php获取远程接口数据,php获取远程接口数据的方法
当前网址:http://cdkjz.cn/article/dsgoidd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220