资讯

精准传达 • 有效沟通

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

php获取远程数据,php下载远程文件到服务器

PHP CURL 获取远程数据下载

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

成都创新互联公司是一家专注于成都网站设计、成都网站建设与策划设计,永丰网站建设哪家好?成都创新互联公司做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:永丰等地区。永丰做网站价格咨询:13518219792

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

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

header('location: $url");

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

setcookie ($cname ,$cvalue);

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

header('location: $url");

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

php连接远程数据库

对于PHP连接远程MySql数据库,通常要使用如下的语句:

var

$serverName

=

'db4free.net:3306';//数据库服务器

var

$dbName

=

'dbname';//数据库名

var

$dbUsername

=

'username';//用户名

var

$dbPassword

=

'123';//登陆密码

mysql_connect($serverName,$dbUsername

,$dbPassword);

mysql_select_db($dbName);

请教,php如何获取远程JSon内容

$ch = curl_init();

//组装用户名和密码

//模拟提交两个数据 可以不提交

$info['username'] = $this-username;//用户名

$info['password'] = $this-pwd;//密码

//模拟表单提交

$params[CURLOPT_URL] = $this-url; //请求url地址

$params[CURLOPT_HEADER] = true; //是否返回响应头信息

$params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回

$params[CURLOPT_FOLLOWLOCATION] = true; //是否重定向

$params[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1';//模拟浏览器

$postfields = '';

//将表单要提交的数据编程URL拼接方式

foreach ($info as $key = $value){

$postfields .= urlencode($key) . '=' . urlencode($value) . '';

}

$params[CURLOPT_POST] = true;//POST方式

$params[CURLOPT_POSTFIELDS] = $postfields;

curl_setopt_array($ch, $params); //传入curl参数

$content = 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获取远程数据,php下载远程文件到服务器
文章起源:http://cdkjz.cn/article/hodpeg.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220