《PHP如何調(diào)用百度AI開放平臺(tái)中NLP語言處理基礎(chǔ)技術(shù)的“依存句法分析”等接口?》要點(diǎn):
本文介紹了PHP如何調(diào)用百度AI開放平臺(tái)中NLP語言處理基礎(chǔ)技術(shù)的“依存句法分析”等接口?,希望對您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:PHP開發(fā)
在百度AI開放平臺(tái)中,PHP如何調(diào)用NLP語言處理基礎(chǔ)技術(shù)等接口?
有兩種模式:
一種是使用百度的SDK,參http://ai.baidu.com/docs#/NLP-PHP-SDK/fddb1eac
一種是使用自己封裝的PHP類,參http://ai.baidu.com/docs#/NLP-API/top
本例展示了使用自己封裝的PHP類調(diào)用百度NLP的句法依存關(guān)系接口:
/** * Class baiduapi 不使用 * 作者:www.snjht.com */ class baiduapi { public $error =''; public $apiUrl = ''; private $_errnum=1; private $header = ["Content-Type: application/json"]; public $access_token=''; //以下這些需要換成你自己的。 public $appId1 = '2321332'; public $appKey1 = 'WLsd3232bI1j7ve3rnCNHIf'; //換成你的APP KEYA public $appSecretKey1 = 'f8w4g123asdfjwP0AExcYFh17PhLjYt1'; //換成你的 public $tokenApi = 'https://aip.baidubce.com/oauth/2.0/token'; public $grant_type = 'client_credentials'; //緩存文件路徑 public $cacheFile = WEBROOT.'public/uploads/modules/tmp'; /** * http constructor. */ public function __construct () { if(!is_dir ($this->cacheFile)) @mkdir ($this->cacheFile); $this->cacheFile = $this->cacheFile.'/baiduapi_token_cache.txt'; $this->access_token = $this->getAccessToken (); } /** * 依存句法分析接口 * @param $mode 默認(rèn)值為0,可選值mode=0(對應(yīng)web模型,適用于處理網(wǎng)頁文本等書面表達(dá)句子);mode=1(對應(yīng)query模型,適用于處理信息需求類的搜索或口語query) */ public function textDependParse($text='', $mode =0) { $this->apiUrl = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/depparser?charset=UTF-8&access_token='.$this->access_token; $post = ['text'=>$text, 'mode'=>$mode]; // mb_convert_encoding(json_encode($text), 'GBK', 'UTF8') $post = json_encode ($post); #關(guān)鍵 $re = $this->getBaiduApi($this->apiUrl, $post , $this->header); return $re; } /** 獲取百度API接口返回值: * @param $url API地址,如拼音調(diào)用, * @param string $auth 你在百度的apiKey * @return mixed 成功則返回std對象 */ public function getBaiduApi($url='', $postData='', $headers=[]) { if(empty($url)) return false; $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $url ); #["apikey: $apiKeyAuth"] if(!empty($headers)) curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers ); curl_setopt ( $ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); //cURL允許執(zhí)行的最長秒數(shù)。這里設(shè)定10S curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); #curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); #curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt ( $ch, CURLOPT_POST, empty($postData)?0:1 ); //POST則設(shè)置為1 if(!empty($postData) ){ if( is_array ($postData)) $postData = http_build_query($postData); curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postData ); //POST方式再啟用 } $result = curl_exec ( $ch ); curl_close ( $ch ); $result = json_decode($result); if( $this-> _errorParse($result)) { if(($result->error_code=='111' || $result->error_code=='110') && $this->_errnum++==1) //Access token過期,重新請求一次 { if(file_exists ($this->cacheFile)) @unlink ($this->cacheFile); $newAccessToken = $this->getAccessToken (); $this->apiUrl = str_replace ($this->access_token,$newAccessToken,$this->apiUrl); $url = str_replace ($this->access_token,$newAccessToken, $url); $this->access_token =$newAccessToken; unset($newAccessToken); $result = $this->getBaiduApi($url, $postData, $headers); }else{ return false; } } return $result; } /** 獲取 access token * @return mixed */ public function getAccessToken() { $cacheFile = file_exists ($this->cacheFile) ? file_get_contents ($this->cacheFile) : ''; if(!empty($cacheFile)) { $json = json_decode ($cacheFile); if( (time ()-$json->starttime) < ($json->expires_in-100) ) return $json->access_token; #2592000 } $post_data['grant_type'] = $this->grant_type; $post_data['client_id'] = $this->appKey1; $post_data['client_secret'] = $this->appSecretKey1; $paraStr = ""; foreach ( $post_data as $k => $v ) { $paraStr .= "$k=" . urlencode( $v ). "&" ; } $paraStr = substr($paraStr ,0,-1); $this->apiUrl = 'https://aip.baidubce.com/oauth/2.0/token'; $this->apiUrl .='?'.$paraStr; #$url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=WL3EGcTOnbI1j7ve3rnCNHIf&client_secret=f8w4gG5GdLLMjwP0AExcYFh17PhLjYt1'; $res = $this->getBaiduApi($this->apiUrl); if(!empty($this->error)){ return false; } $res->starttime = time (); file_put_contents ($this->cacheFile ,json_encode ($res, true)); return $res->access_token; } private function _errorParse($res) { $errors=[ '1'=> 'Unknown error 服務(wù)器內(nèi)部錯(cuò)誤,請?jiān)俅握埱螅绻掷m(xù)出現(xiàn)此類錯(cuò)誤,請通過QQ群(224994340)或工單聯(lián)系技術(shù)支持團(tuán)隊(duì)', '2'=>'Service temporarily unavailable 服務(wù)暫不可用,請?jiān)俅握埱螅绻掷m(xù)出現(xiàn)此類錯(cuò)誤,請通過QQ群(224994340)或工單聯(lián)系技術(shù)支持團(tuán)隊(duì)', '3'=>'Unsupported openapi method 調(diào)用的API不存在,請檢查后重新嘗試', '4'=>'Open api request limit reached 集群超限額', '6'=>'No permission to access data 無權(quán)限訪問該用戶數(shù)據(jù)', '17'=>'Open api daily request limit reached 每天請求量超限額', '18'=>'Open api qps request limit reached QPS超限額', '19'=>'Open api total request limit reached 請求總量超限額', '100'=>'Invalid parameter 包含了無效或錯(cuò)誤參數(shù),請檢查代碼', '110'=>'Access token invalid or no longer valid: Access Token失效', '111'=>'Access token expired: Access token過期', '282000'=>'internal error 服務(wù)器內(nèi)部錯(cuò)誤,請?jiān)俅握埱螅?nbsp;如果持續(xù)出現(xiàn)此類錯(cuò)誤,請通過QQ群(632426386)或工單聯(lián)系技術(shù)支持團(tuán)隊(duì)。', '282002'=>'input encoding error 編碼錯(cuò)誤,請使用GBK編碼', '282004'=>'invalid parameter(s) 請求中包含非法參數(shù),請檢查后重新嘗試', '282130'=>'no result 當(dāng)前查詢無結(jié)果返回,出現(xiàn)此問題的原因一般為:參數(shù)配置存在問題,請檢查后重新嘗試', '282131'=>'input text too long 輸入長度超限,請查看文檔說明', '282133'=>'param {參數(shù)名} not exist 接口參數(shù)缺失', '282300'=>'word error: word不在算法詞典中', '282301'=>'word_1 error word_1 提交的詞匯暫未收錄,無法比對相似度', '282302'=>'word_2 error: word_2 提交的詞匯暫未收錄,無法比對相似度', '282303'=>'word_1&word_2 error : word_1和word_2暫未收錄,無法比對相似度', ]; $this->error = ''; if (!empty($res->error_code) && !empty($errors[$res->error_code])) { $this->error = $errors[$res->error_code]; } return !empty($this->error) ? $this->error : false; } }
調(diào)用示例:
$re = $baidu->textDependParse('3D滾輪瘦臉儀微電流V臉神器臉部按摩器提拉緊致面部按摩儀美容儀');
if(!$re) echo $baidu->error;
else print_r ($re);
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/14680.html