1. 幾種php訪問url的方法
常用的就三種吧,
file_get_contents(), fopen, curl
一般用 curl 擴展的比較多,除此以外還有其他方法
fsockopen 啥的
2. 如何用php調用外部介面json數據
一般使用php發送請求,獲取返回的數據,進行解析;
<?php
$url="介面地址";
//發送請求獲取返回值,file_get_contents只支持get請求,post使用curl
$json = file_get_contents($url);
//把json數據轉化成數組
$data = json_decode($json,true);
//列印看看
print_r($data);
?>
3. php打開URL的幾種方法
PHP中打開URL地址的幾種方法總結,這里的函數主要用於小偷採集等函數。
1:用file_get_contents
以get方式獲取內容
復制代碼代碼如下:
<?php
$url='http://www..com/';
$html=file_get_contents($url);
//print_r($http_response_header);
ec($html);
printhr();
printarr($http_response_header);
printhr();
?>
示例代碼2:用fopen打開url,
以get方式獲取內容
復制代碼代碼如下:
<?
$fp=fopen($url,'r');
printarr(stream_get_meta_data($fp));
printhr();
while(!feof($fp)){
$result.=fgets($fp,1024);
}
echo"urlbody:$result";
printhr();
fclose($fp);
?>
示例代碼3:用file_get_contents函數,以post方式獲取url
復制代碼代碼如下:
<?php
$data=array('foo'=>
'bar');
$data=http_build_query($data);
$opts=array(
'http'
=>array(
'method'=>'POST',
'header'=>"Content-type:
application/x-www-form-urlencoded".
"Content-Length:".strlen($data).
"",
'content'=>$data
),
);
$context=
stream_context_create($opts);
$html=
file_get_contents('http://localhost/e/admin/test.html',false,$context);
echo$html;
?>
示例代碼4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body
復制代碼代碼如下:
<?
functionget_url
($url,$cookie=false){
$url=parse_url($url);
$query=
$url[path]."?".$url[query];
ec("Query:".$query);
$fp=fsockopen(
$url[host],$url[port]?$url[port]:80,$errno,$errstr,30);
if(!$fp){
returnfalse;
}else{
$request="GET$queryHTTP/1.1";
$request.="Host:$url[host]";
$request.="Connection:Close";
if($cookie)$request.="Cookie:$cookie ";
$request.="";
fwrite($fp,$request);
while(!@feof($fp)){
$result.=@fgets($fp,
1024);
}
fclose($fp);
return$result;
}
}
//獲取url的html部分,去掉header
functionGetUrlHTML($url,$cookie=false){
$rowdata=get_url($url,$cookie);
if($rowdata)
{
$body=
stristr($rowdata,"");
$body=substr($body,4,strlen($body));
return$body;
}
returnfalse;
}
?>
4. php 如何執行url
PHP:
header('Location: http://www.example.com/');
file_get_contents(http://it.sohu.com/7/1002/17/column20466721_3257.shtml);
5. PHP調用資料庫中的URL地址進行跳轉問題
在跳轉的時候php一般默認你用的是相對地址所以會把域名自動加上,所以在存儲地址的時候一般要把http://加上。http://www..com
6. php如何訪問帶參數的url(介面設定是訪問這個帶參數的url會返回一個xml文件),並完成對xml文件的讀取
fopen、file_get_contents都可以,詳細網路: php 打開url
7. php如何通過url調用php文件中的方法
題主所描述的這種形式,是MVC設計模式的典型應用。
通過使用PSR4來實現自動載入,可以通過處理路由來實現
//處理路由的方法
staticpublicfunctionroute()
{
//獲取的模塊
$_GET['m']=isset($_GET['m'])?$_GET['m']:'Index';
//獲取行為動作action又叫方法
$_GET['a']=isset($_GET['a'])?$_GET['a']:'index';
$controller='Controller\'.$_GET['m'].'Controller';
//echo$controller;
$c=new$controller();
//$c->$_GET['a']();
call_user_func(array($c,$_GET['a']));
}
最終可實現以下形式:
8. php怎麼調用其他網站提供的api 介面
在這里openUser.php相當於一個介面,其中get_user_list 是一個API(獲取用戶列表),講求返回的數據類型為JSON格式。
需要在PHP代碼中執行這條鏈接他就會返回。
GET方式的直接使用
$file_contents = file_get_content('http://localhost/openUser.php?act=get_user_list&type=json')
POST方式得用下面的。
$url = 'http://localhost/openUser.php?act=get_user_list&type=json';
$ch = acurl_init ();
acurl_setopt ( $ch, CURLOPT_URL, $url );
acurl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
acurl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
acurl_setopt ( $ch, CURLOPT_POST, 1 ); //啟用POST提交
$file_contents = curl_exec ( $ch );
9. php中想調用url後的參數進入另一個鏈接中,不知道如何操作
方法是
<img src="<? echo $_GET['num'] ?>.gif" />
其中$_GET['num']等於你圖片的名字。
不過PHP只能實現需要刷新頁面的。
如果需要無刷新的就只能用JS了。