php环境下使用联通接口获取手机号号码,并输出json格式文件

这里用的tp5.1实现的,这里放上实现功能的核心代码。

<?php
namespace app\ordertj\controller;
use think\Controller;
header('Access-Control-Allow-Origin:*');//允许跨域
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept');//允许跨域
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');//允许跨域

class Ordertj extends  Controller
{

//根据前端提交的参数获取数据,输出json文件开始

  public function testurl(){
     $citycode=input('post.citycode');   
  $provicecode=input('post.provicecode');
     /*$citycode='721';  //城市编码 
    $provicecode='71' ;*/  //省份编码
   // $provicecode=input('post.provicecode');

    $json_string = file_get_contents('static//numinfo/'.$provicecode.'.json');
    $data = json_decode($json_string,true);

    // var_dump($data);
   foreach ($data as $key => $value) {
    
         if(in_array($citycode,$value)){
           
            return json($value);
            exit;
          //return 
        }
   }

  }

//根据前端提交的参数获取数据,输出json文件结束

//号码获取开始
///AES加密
 function encrypt($data,$key){
    $data = openssl_encrypt($data, 'aes-128-ecb', base64_decode($key), OPENSSL_RAW_DATA);
    return base64_encode($data);
}

 function sign($key,$dataStr){

    return base64_encode(hash_hmac("md5", $dataStr, base64_decode($key), true));
}

//城市编码表获取开始

 function getcitycode($Url,$data_string){

//通过curl_init发送请求,获取数据
  $ch = curl_init();//初始化curl
 curl_setopt($ch, CURLOPT_URL,$Url);//抓取指定网页
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)
)); 

       $result = curl_exec($ch);//运行curl 获取的此接口数据为json数据

        if (curl_errno($ch)) {
    print curl_error($ch);
}
        curl_close($ch);
  // var_dump(json_decode($result,true));
   $arrnum= json_decode($result,true); //将json数据转化为数组

    $allcity=$arrnum['body'];
    return $allcity;

//$json_city = json_encode($allcity,JSON_UNESCAPED_UNICODE);  //JSON_UNESCAPED_UNICODE可以使中文不转义
// 写入文件
//file_put_contents('static/json/citycode.json', $json_city);

}

//城市编码表获取结束


       public function getphonenum($prefix=''){

  $Url='http://cd.10010.com/zop/link/num/select/v1'; //生产环境请求地址

//这里的参数需要接口方提供
$APPCODE='C3B3U80C38724C85TT8AC2977EC37160';
   
  $HMACMD5='ywt0UqK2uqLULp1/TEBH2G8St1u71fANXOovX0ll63ctDqxBWkmHKTUzoY7wVxL+7xUQqCxOBIqeST9CGmLtDQ=='; 
  $AES='bufB2aTrsiKRe41hkO9adf==';

//这里的参数需要接口方提供

//$nowtm= time();
$timestamp=date('Y-m-d H:i:s.B',time());
  
$chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        $uuid=$prefix.$uuid;

$dataStr='appCode'.$APPCODE.'timestamp'.$timestamp."uuid".$uuid.$HMACMD5;
// 获取sign
//身份循环开始
//$arr=[10,11,13,17,18,19,30,31,34,36,38,50,51,59,70,71,74,75,76,79,81,83,84,85,86,87,88,89,90,91,97]; //省粉编码
//foreach ($arr as $rowsfcode) {
//$provicecode=$rowsfcode;

$provicecode='97';
 $sign=$this->sign($HMACMD5,$dataStr);

$head["sign"]=$sign;
$head["timestamp"]=$timestamp;
$head["uuid"]=$uuid;
//$body["provinceCode"]='34';
$body["provinceCode"]=$provicecode;

$Url2='http://cd.10010.com/zop//link/king/postInfo/qry'; 
//$Url2='http://demo.mall.10010.com:8104//zop//link/king/postInfo/qry'; 
$reqObjcity["head"]=$head;
$reqObjcity["body"]=$body;
$reqObj3=$this->encrypt(json_encode($reqObjcity),$AES);

$data_city["appCode"]=$APPCODE;
$data_city["reqObj"]=$reqObj3;
$data_city=json_encode($data_city);

$citycode=$this->getcitycode($Url2,$data_city);


foreach ($citycode as $rowcity) {
   $cityrecodearr['cscode']=$rowcity['numCityCode'];
    $cityrecodearr['cityname']=$rowcity['postCityName'];
    $cityrecode[]=$cityrecodearr;

} 

 $wycitycode= array_unique($cityrecode,SORT_REGULAR ); //多维数组去重,需要加参数SORT_REGULAR

//城市编码循环开始

foreach ($wycitycode as $rowcode) { 
  //$hbarr[]='';

$APPCODE='C3B3F80C30724C72BB8AC2977EC37160';
   
  $HMACMD5='ywa0UqY2uqLULp1/ZEBH2G8St1u71fANXOovX0ll63ctDqxBWkmHKTUzoY7wVxL+7xUQqCxOBIqeST9CGmLtDQ==';
  $AES='bufB2aUrsiNCa41TJO8adw==';

//循环城市时再次获取新的head值与body值,避免时间差导致的错误开始
$provicecode='97';
$timestamp=date('Y-m-d H:i:s.B',time());
  
$chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        $uuid=$prefix.$uuid;

$dataStr='appCode'.$APPCODE.'timestamp'.$timestamp."uuid".$uuid.$HMACMD5;
$sign=$this->sign($HMACMD5,$dataStr);

$head["sign"]=$sign;
$head["timestamp"]=$timestamp;
$head["uuid"]=$uuid;
//$body["provinceCode"]='34';
$body["provinceCode"]=$provicecode;

//循环城市时再次获取新的head值与body值,避免时间差导致的错误结束

$codecs=$rowcode['cscode'];
$cityname=$rowcode['cityname'];
//$body["cityCode"]="710";
$body["cityCode"]=$codecs;

//$body["goodsId"]="981609180703"; //测试商品
$body["goodsId"]="981610241535"; //goodsId参数有接口方提供
$body["qryType"]="02";
$body["searchCategory"]="3";
$body["searchType"]="02";
$body["searchValue"]="12";
$body["amounts"]="100";

$reqObj['head']=$head;
$reqObj['body']=$body;


$reqObj2=$this->encrypt(json_encode($reqObj),$AES); //需要对$reqObj使用json_encode才能使用ase加密方法,否则加密不成功,猜想是不是使用aes加密的数据只能是json格式

//var_dump($reqObj2);

$data_string["appCode"]=$APPCODE;
$data_string["reqObj"]=$reqObj2;
$data_string=json_encode($data_string); //这一步也需要转化为json格式,否则提示报文不符合规范。

//连续获取10次数据开始
for ($i=0; $i < 10; $i++) {  
  # code...

  $ch = curl_init();//初始化curl
 curl_setopt($ch, CURLOPT_URL,$Url);//抓取指定网页
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)
)); 

       $result = curl_exec($ch);//运行curl 获取的此接口数据为json数据

        if (curl_errno($ch)) {
    print curl_error($ch);
}
        curl_close($ch);

       // echo $result;

   $arrnum= json_decode($result,true); //将json数据转化为数组
  
     //   var_dump($arrnum["body"]["numArray"]);
           $numarrs=$arrnum["body"]["numArray"];
           
        $numarr=array_chunk($numarrs,12,true);
            foreach($numarr as $rownum){

              $hbarr[]=array_values($rownum);
            }
      
  }
//连续获取10次数据结束
       

        $numatt['cityname']=$cityname;
        $numatt['numcitycode']=$codecs;
        $numatt['numinfo']=$hbarr;
       $numallar[]=$numatt;

 }




$json_ctystring = json_encode($numallar,JSON_UNESCAPED_UNICODE);
file_put_contents('static/numinfo/'.$provicecode.'.json', $json_ctystring);
      //var_dump($numallar[0]['numinfo']);
     // echo count(($numallar[0]['numinfo']));
 //城市编码循环结束    
 

//省份循环结束

}

//号码获取结束


}

 

上一篇:ubuntu扩容


下一篇:linux查看网卡uuid