本文目錄一覽:
- 1、php中的API接口怎麼寫 ?
- 2、什麼是API接口,PHP開發API接口的例子
- 3、php中如果去開發一個api
- 4、php如何開發API接口
- 5、如何用PHP開發API接口
- 6、如何給PHP程序開發API
php中的API接口怎麼寫 ?
以我目前的能力沒法理解這個問題。但我舉個例子,可能並不是你想要的答案,但沒準可以提醒一下。我們用GOOGLE MAP API (開源)的時候,會引用一個類似接口的方法,方法有很多參數可以設置。比如這是個引用地圖的方法,它的裡面寫了很多完整的方法,我這裡就不放出來了,但是我舉得例子是參數傳遞在最上面的initialize () (這裡我們暫時當作接口)方法上,我們可以傳遞interface_zoom即縮放大小,interface_lati,interface_lngi就是經緯度等等參數,這些參數可以由我們自己處理,無論是從database還是什麼其他方法讀取還是怎麼運算,然後call這個initialize方法帶指定參數就可以了,具體裡面如何操作的地圖,如何生成的地圖,我們就不管了。接口就是我們看到日常生活中的插座,它規定好了規格,是幾項的插頭,我們就做成幾項的插頭,具體裡面什麼線路我們不管。方法中規定了什麼類型的參數,我們就傳什麼類型的參數,具體裡面有什麼算法我們不管。個人最自白的解釋,與官方有出入。
var map;
var infoWindow;
function initialize(interface_zoom,interface_lati,interface_lngi) {
var mapDiv = document.getElementById(‘map-canvas’);
map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(interface_lati, interface_lngi),
zoom: interface_zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
……
……
}
什麼是API接口,PHP開發API接口的例子
就是php只處理數據,而不直接輸出html
把視圖層都交給js去完成。
比如:
//獲取數據
$id=$_GET[‘id’];
if($id){
//支數據庫取數據
$sql=”select*from table where id='”.$id.”‘”;
$re=…..;
echo json_encode($re);//返回json格式數據給前端處理
}
//修改數據
if($id$_GET[‘edit’]){
////數據庫操作
echo “修改成功”;
}
php中如果去開發一個api
你看看這段代碼 ,如果想寫Ucenter接口很方便
?php
/**
* api控制器基類
*/
class ApiApp extends ECBaseApp
{
function _init_visitor()
{
$this-visitor = env(‘visitor’, new ApiVisitor());
}
/**
* 執行登陸操作
* 這個函數要跟 frontend.base.php 中的 _do_login 保持一致
*/
function _do_login($user_id)
{
$mod_user = m(‘member’);
$user_info = $mod_user-get(array(
‘conditions’ = “user_id = ‘{$user_id}'”,
‘join’ = ‘has_store’,
‘fields’ = ‘user_id, user_name, reg_time, last_login, last_ip, store_id’,
));
/* 店鋪ID */
$my_store = empty($user_info[‘store_id’]) ? 0 : $user_info[‘store_id’];
/* 保證基礎數據整潔 */
unset($user_info[‘store_id’]);
/* 分派身份 */
$this-visitor-assign($user_info);
/* 更新用戶登錄信息 */
$mod_user-edit(“user_id = ‘{$user_id}'”, “last_login = ‘” . gmtime() . “‘, last_ip = ‘” . real_ip() . “‘, logins = logins + 1”);
/* 更新購物車中的數據 */
$mod_cart = m(‘cart’);
$mod_cart-edit(“(user_id = ‘{$user_id}’ OR session_id = ‘” . SESS_ID . “‘) AND store_id ‘{$my_store}'”, array(
‘user_id’ = $user_id,
‘session_id’ = SESS_ID,
));
}
/**
* 執行退出操作
*/
function _do_logout()
{
$this-visitor-logout();
}
}
/**
* api訪問者
*/
class ApiVisitor extends BaseVisitor
{
var $_info_key = ‘user_info’;
}
?
php如何開發API接口
進入php源程序目錄中的ext目錄中,這裡存放着各個擴展模塊的源代碼,選擇你需要的模塊,比如curl模塊:cd curl
執行phpize生成編譯文件,phpize在PHP安裝目錄的bin目錄下
/usr/local/php5/bin/phpize
運行時,可能會報錯:Cannot find autoconf. Please check your autoconf installation and
the $PHP_AUTOCONF
environment variable is set correctly and then rerun this
script.,需要安裝autoconf:
yum install autoconf(RedHat或者CentOS)、apt-get install
autoconf(Ubuntu Linux)
/usr/local/php5/bin/php -v
執行這個命令時,php會去檢查配置文件是否正確,如果有配置錯誤,
這裡會報錯,可以根據錯誤信息去排查!
如何用PHP開發API接口
他會提供相應接口給你的,具體調用方法就相當於講求某個鏈接。act=get_user_listtype=json在這裡operate.php相當於一個接口,其中get_user_list 是一個API(獲取用戶列表),講求返回的數據類型為JSON格式。
act=get_user_listtype=json’;$ch = curl_init ();curl_setopt ( $ch, CURLOPT_URL, $url );curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );curl_setopt ( $ch, CURLOPT_POST, 1 ); //啟用POST提交$file_contents = curl_exec ( $ch );curl_close ( $ch );
如何給PHP程序開發API
具體代碼如下:
?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ”);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?
PHP 獨特的語法混合了C、Java、Perl以及PHP自創的語法。
它可以比CGI或者Perl更快速地執行動態網頁。用PHP做出的動態頁面與其他的編程語言相比,PHP是將程序嵌入到HTML(標準通用標記語言下的一個應用)文檔中去執行,
執行效率比完全生成HTML標記的CGI要高許多;
PHP還可以執行編譯後代碼,編譯可以達到加密和優化代碼運行,使代碼運行更快。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/244342.html