⑴ php獲取遠程頁面問題
用file()這個函數也可以,你可以試試
⑵ 請問php如何像打開本地文件一樣打開遠程ftp伺服器上的文件
<?php
$handle = fopen("/home/rasmus/file.txt", "r");
$handle = fopen("/home/rasmus/file.gif", "wb");
$handle = fopen("http://www.example.com/", "r");
$handle = fopen("ftp://user:[email protected]/somefile.txt", "w");
?>
這樣不就好了,
'r' 只讀方式打開,將文件指針指向文件頭。
'r+' 讀寫方式打開,將文件指針指向文件頭。
'w' 寫入方式打開,將文件指針指向文件頭並將文件大小截為零。如果文件不存在則嘗試創建之。
'w+' 讀寫方式打開,將文件指針指向文件頭並將文件大小截為零。如果文件不存在則嘗試創建之。
'a' 寫入方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創建之。
'a+' 讀寫方式打開,將文件指針指向文件末尾。如果文件不存在則嘗試創建之。
之後你就可以直接查詢php的手冊 ,,fopen fwrite file_get_content file_put_content 這幾個函數就好了
⑶ php連接遠程資料庫
在php中如果要連接遠程資料庫連接方法很簡單,只要把本地連接localhost或127.0.0.1改成指定遠程伺服器一IP地址或者直接域名即可。
語法
mysql_connect(servername,username,password);
例子
在下面的例子中,我們在一個變數中($con)存放了在腳本中供稍後使用的連接。如果連接失敗,將執行"die"部分:
代碼如下:
<?php
$con=mysql_connect("localhost","peter","abc123");
if(!$con)
{
die('Couldnotconnect:'.mysql_error());
}
//somecode
?>
上面是連接本地資料庫,下面把localhost改成遠程IP即可了
實例 代碼如下:
$conn=mysql_connect('http://boaer.com','root','123456888');
if(!$conn)echo"失敗!";
elseecho"成功!";
//從表中提取信息的sql語句
$sql="SELECT*FROMuserwhereuserName='$user_name'";
//執行sql查詢
$result=mysql_db_query('info',$sql,$conn);
//獲取查詢結果
$row=mysql_fetch_row($result);
mysql_close();
⑷ php中有哪些常用的遠程請求發送方法
1、用file_get_contents 以get方式獲取內容:
<?php
$url = 'http://www.maizie.com/' ;
$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( 'http://www.maizie.com' , 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: //www.maizie.com/');
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如何像打開本地文件一樣打開遠程ftp伺服器上的文件
通過FTP是沒辦法遠程打開文件的 ,他只是一個上傳下載的工具
⑹ 如何通過php連接遠程主機並操作
一、最佳方案是在77機器上安裝apache和php
二、可以通過管道控制使用telnet登錄77號機執行命令並獲取結果,ssh連接本人沒有做過,telnet方法如下:
$f=fopen("telnet://192.168.0.77","rw");
$s=fgets($fp);
fputs($fp,"root");
$s=fgets($fp);
fputs($fp,"pass");
$s=fgets($fp);
fputs($fp,"df -h");
while(!feof($fp)){
$fp=fgets($fp);
echo $s;
}
fclose($fp);
手機輸入真累~~~
⑺ 如何遠程打開資料庫文件。例如:織夢鏈接資料庫文件是:common.inc.php 我指導鏈接地址,怎麼遠程打開
php是在伺服器端運行後,解釋成html顯示在客戶端的,有的php頁面就做輸出的,所以你鍵入地址,伺服器解釋出html後,你可以看到顯示結果,有些純後台代碼運行的,所以你鍵入地址,也無法看到。向這種資料庫連接定義,全局變數定義的文件等等,即使你知道文件名,也無法看到的