在php中如果要連接遠(yuǎn)程數(shù)據(jù)庫連接方法很簡單,只要把本地連接localhost或127.0.0.1改成指定遠(yuǎn)程服務(wù)器一IP地址或者直接域名即可。

10年積累的成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先建設(shè)網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有納溪免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
語法
mysql_connect(servername,username,password);
例子
在下面的例子中,我們在一個變量中?($con)?存放了在腳本中供稍后使用的連接。如果連接失敗,將執(zhí)行?"die"?部分:
代碼如下:
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
//?some?code
?
上面是連接本地數(shù)據(jù)庫,下面把localhost改成遠(yuǎn)程IP即可了
實例 代碼如下:
$conn=mysql_connect('','root','123456888');
if(!$conn)?echo?"失敗!";
else?echo?"成功!";
//?從表中提取信息的sql語句
$sql="SELECT?*?FROM?user?where?userName='$user_name'";
//?執(zhí)行sql查詢
$result=mysql_db_query('info',?$sql,?$conn);
//?獲取查詢結(jié)果
$row=mysql_fetch_row($result);
mysql_close();
$data = file_get_contents($url);//目的頁面內(nèi)容獲取
$t = json_decode($data,1);//轉(zhuǎn)換為PHP數(shù)組
//處理...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlo);//數(shù)據(jù)發(fā)送地址
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//發(fā)送的數(shù)據(jù)數(shù)組
curl_exec($ch);
1、用file_get_contents 以get方式獲取內(nèi)容:
?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方式獲取內(nèi)容:
$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函數(shù)打開url,以get方式獲取完整的數(shù)據(jù),包括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函數(shù)打開url,以POST方式獲取完整的數(shù)據(jù),包括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是否已經(jīng)打開了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中遠(yuǎn)程請求方法,希望對php新人的學(xué)習(xí)、工作有一定的幫助。當(dāng)然遠(yuǎn)程請求的方法肯定不止題主上面為大家介紹的這6中,如果你還有更好的方法,歡迎補充分享。軟件開發(fā)的學(xué)習(xí),就是一個分享式的學(xué)習(xí),讓我們一起在分享學(xué)習(xí)中,共進(jìn)步。
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));
?
輸出結(jié)果:
這樣做肯定是用的你的帶寬,是把文件下載到你的服務(wù)器上,然后再下載給客戶端。
有兩條路你可以去試試看,我沒做過:一是setcookie指定域名是那個網(wǎng)站,然后轉(zhuǎn)向:
setcookie ($cname ,$cvalue ,$expire ,$path , $host);
header('location: $url");
另外一個方法類似,好像有個P3P可以傳遞COOKIE,需要你自己查資料:
setcookie ($cname ,$cvalue);
header('P3P: ....');
header('location: $url");
第二個辦法應(yīng)該是可以的,陶寶和開心網(wǎng)都在用這樣的技術(shù),陶寶有許多域名,一次登錄后都可以使用,就是利用P3P實現(xiàn)的COOKIE傳遞。
兩種比較簡單的方法:
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獲取遠(yuǎn)程接口數(shù)據(jù),php獲取遠(yuǎn)程接口數(shù)據(jù)的方法
標(biāo)題路徑:http://chinadenli.net/article11/dsgoidd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、Google、網(wǎng)站改版、企業(yè)網(wǎng)站制作、企業(yè)建站、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)