= "POST";$url
= "/common/ISBN/queryISBN";$headers
= NULL;$params
= array("ISBN"=>"" //ISBN书号,如“9787530212837”);$result
= apishop_curl($method, $url, $headers, $params);If
($result) {$body = json_decode($result["body"], TRUE);$status_code = $body["statusCode"];If ($status_code == "000000") {//状态码为000000,说明请求成功echo "请求成功:" .
$result["body"];} else {//状态码非000000,说明请求失败echo "请求失败:" .
$result["body"];}}
else {//返回内容异常,发送请求失败,以下可根据业务逻辑自行修改echo "发送请求失败";}/***转发请求到目的主机* @param $method string请求方法* @param $URL string请求地址* @param null $headers请求头* @param null $param请求参数* @return array|bool*/function
apishop_curl(&$method, &$URL, &$headers = NULL, &$param = NULL){//初始化请求$require = curl_init($URL);//判断是否HTTPS$isHttps = substr($URL, 0, 8) == "https://" ? TRUE : FALSE;//设置请求方式switch ($method) {case "GET":curl_setopt($require, CURLOPT_CUSTOMREQUEST, "GET");break;case "POST":curl_setopt($require, CURLOPT_CUSTOMREQUEST, "POST");break;default:return FALSE;}if ($param) {curl_setopt($require, CURLOPT_POSTFIELDS, $param);}if ($isHttps) {//跳过证书检查curl_setopt($require, CURLOPT_SSL_VERIFYPEER, FALSE);//检查证书中是否设置域名curl_setopt($require, CURLOPT_SSL_VERIFYHOST, 2);}if ($headers) {//设置请求头curl_setopt($require, CURLOPT_HTTPHEADER, $headers);}//返回结果不直接输出curl_setopt($require, CURLOPT_RETURNTRANSFER, TRUE);//重定向curl_setopt($require, CURLOPT_FOLLOWLOCATION, TRUE);//把返回头包含再输出中curl_setopt($require, CURLOPT_HEADER, TRUE);//发送请求$response = curl_exec($require);//获取头部长度$headerSize = curl_getinfo($require, CURLINFO_HEADER_SIZE);//关闭请求curl_close($require);if ($response) {//返回头部字符串$header = substr($response, 0, $headerSize);//返回体$body = substr($response, $headerSize);//过滤隐藏非法字符$bodyTemp = json_encode(array(0 => $body));$bodyTemp = str_replace("", "", $bodyTemp);$bodyTemp = json_decode($bodyTemp, TRUE);$body = trim($bodyTemp[0]);//将返回结果头部转成数组$respondHeaders = array();$header_rows = array_filter(explode(PHP_EOL, $header), "trim");foreach ($header_rows as $row) {$keylen = strpos($row, ":");if ($keylen) {$respondHeaders[] = array("key" => substr($row, 0, $keylen),"value" => trim(substr($row, $keylen + 1)));}}return array("headers" => $respondHeaders,"body" => $body);} else {return FALSE;}}
如果觉得《isbn书号查询php代码 eoLinker-API Shop ISBN书号查询 PHP调用示例代码》对你有帮助,请点赞、收藏,并留下你的观点哦!