PHP Learning notes: Using the Baidu API to achieve the mobile phone attribution query

Source: Internet
Author: User

Never used the API, do not know how to get the API data, followed by the demo, and then modify, finally realize the mobile phone number query function, code and instructions are very full, we try.

<?php/** * Created by Jianqingwang * User:administrator * DATE:2016/9/10 0010 * time:22:49 * Query Phone Attribution *    /Header (" content-type:text/html; Charset=utf-8 ")///$phoneNum =$_get[' phonenum ']; $phoneNum = ' 18807457777 ';//query Phone number attribution function, you can directly use function Checkphone ($phoneNum) {    //below starts Baidu's demo    $ch = Curl_init ();    Or in this way: $url = "http://apis.baidu.com/apistore/mobilenumber/mobilenumber?phone= $phoneNum";    $url = ' http://apis.baidu.com/apistore/mobilenumber/mobilenumber?phone= '. $phoneNum;    $header = Array (        ' Apikey: Key Account ',//This is my key account, you can apply to the Baidu website
        Baidu API official Http://apistore.baidu.com/astore/toolshttpproxy?apiId=usu9zw&isAworks=1);    Add Apikey to Header curl_setopt ($ch, Curlopt_httpheader, $header);    curl_setopt ($ch, Curlopt_returntransfer, 1);    Execute HTTP request curl_setopt ($ch, Curlopt_url, $url);    Baidu API Demo End//transcode the returned data to ensure that the data is Utf-8 $data =iconv (' GB2312 ', ' UTF-8 ', Curl_exec ($ch));    To transcode the returned data, convert the JSON to an array, and then true if it does not add to the object, you can first print the output, look at its structure $res = Json_decode (Curl_exec ($ch), true);    $phone = $res [' retdata '] [' phone '];    $prefix = $res [' retdata '] [' prefix '];    $supplier = $res [' retdata '] [' Supplier '];    $province = $res [' retdata '] [' Province '];    $city = $res [' retdata '] [' City '];    $suit = $res [' retdata '] [' suit ']; Turn the data of the query into an array to return to the $checkData [0]= $phone;//The telephone number of the query $checkData [1]= $prefix;//card number segment, can be saved up later data can be used directly in the number segment to query the place of the telephone $checkData [2]= $supplier;//card operators, such as China Mobile $checkData [3]= $province;//card belonging to the province $checkData [4]= $city;//card belonging to the city $checkData [5]= $suit;//Card , such as: 157 card return $checkData;//Return the data after the query, in order to facilitateReturns multiple data, using an array} $myPhoneData =checkphone ($phoneNum); echo $myPhoneData [0]. " Attribution number segment ". $myPhoneData [1]." Operator ". $myPhoneData [2]." Attribution to the province ". $myPhoneData [3]." Attribution to the city ". $myPhoneData [4]." Type of card ". $myPhoneData [5];;/ /output Query Result?>

Effect Show:

I'll consider writing a version with a database when I have time.

PHP Learning notes: Using the Baidu API to achieve the mobile phone attribution query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.