企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
``` <?php header('Content-Type:application/json; charset=utf-8'); class CompanySearch { public $searchKey=""; public $Key =""; private $SecretKey=""; public function __construct(){ $this->key="xxxxxx"; $this->SecretKey="xxxxxxxx"; } public function goSearch($searchKey, $provinceCode=null, $cityCode=null, $pageSize=null, $pageIndex=null){ $optional=""; if (!empty($provinceCode)) { $optional .= "&provinceCode=".$provinceCode; } if (!empty($cityCode)) { $optional .= "&cityCode=".$cityCode; } if (!empty($pageSize)) { $optional .= "&pageSize=".$pageSize; } if (!empty($pageIndex)) { $optional .= "&pageIndex=".$pageIndex; } $this->searchKey=$searchKey; $url="https://api.qichacha.com/FuzzySearch/GetList?key=".$this->key."&searchKey=".$this->searchKey.$optional; return $this->curl($url); } /** * url方式请求 * * @param string $url 要请求的url地址 * @param array $data 要发送的数据 存在该参数就启用post提交,否则为get请求 * @return mixed $output 返回请求的结果 */ private function curl($url, $data = null){ // 1.创建一个新的CURL资源 $ch = curl_init(); // 2.设置URL相应的选项 curl_setopt($ch, CURLOPT_URL, $url); // 设置请求的URL地址 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出(0为直接输出) //curl_setopt($ch, CURLOPT_HEADER, 0); // 启用时会将头文件的信息作为数据流输出 curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置cURL允许执行的最长秒数 curl_setopt($ch, CURLOPT_POST, $data ? 1 : 0); // 存在data就启用post发送 启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。 if (!empty($data)) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } $time=time(); $headerArray=array( "Content-type:application/json;charset='utf-8'", "Accept:application/json", "Token:".strtoupper(md5($this->key.$time.$this->SecretKey)), "Timespan:".$time, ); curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray); //忽略证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 // 3.抓去URL并将它传递给浏览器 $output = curl_exec($ch); if ($output==false) { return curl_error($ch); curl_close($ch); } /*$aStatus = curl_getinfo($ch); if(intval($aStatus["http_code"])==200){ return $sContent; }else{ return false; }*/ // 4.关闭CURL资源,并且释放系统资源 curl_close($ch); // 返回输出 return $output; return json_encode($output); } } $get=$_GET; $searchKey=$get['searchKey']; $provinceCode=$get['provinceCode']??null; $cityCode=$get['cityCode']??null; $pageSize=$get['pageSize']??null; $pageIndex=$get['pageIndex']??null; $cs=new \CompanySearch(); $result=$cs->goSearch($searchKey, $provinceCode, $cityCode, $pageSize, $pageIndex); echo $result; ```