Introduction of API Store
API store is an API aggregation platform that can provide a variety of data services to the outside, more representative of the Dev store, Baidu API store and aggregated data, and so on, such services through the provision of the rest API interface to expose to third party developers.
API Store provides many types of data services, there are common life, application development, financial management, travel tickets and other APIs, but some interfaces are charged, but also provides a lot of free interface, here with mobile phone number attribution to query as a demo to do API call show. second, shell support for the rest interface
In dealing with the RESTful API, Java has always been the strength, especially in recent years the popular Spring boot framework, the concept of microservices has become a popular trend. Here's a more lightweight shell to invoke the API interface, and review the shell script.
When the shell handles the RESTful API, the shell supports the Curl command, and the Curl command provides good support for HTTP impersonation to send HTTP requests. While processing the JSON data, the shell provides the JQ tool, which gives a good explanation of the JSON support. Third, the Code realization idea 1. Mobile phone number attribution to the API
This article calls the API from the Baidu API store, its official published API interface is as follows:
Curl--get--include
' http://apis.baidu.com/apistore/mobilenumber/mobilenumber?phone=15210011578 '-h ' apikey: Your Apikey '
2.JSON Return Example:
{
"Errnum": 0,
"retmsg": "Success",
"Retdata": {
"phone": "15210011578",
"prefix": "1521001",
"Supplier": "Mobile",
"province": "Beijing",
"City": "Beijing",
"Suit": "152 Card"
}
}
3. Parsing of returned result JSON
(1) installation and use of JQ:
installation command for Mac: Brew install JQ
CentOS or Ubuntu installation command:
Apt-get-qq-y Install JQ | | Yum-y Install JQ (2) JSONSTR represents the JSON return example above.
Echo $jsonstr | JQ '.' ා format JSON and verify whether the JSON string is legal
(3) Extracting field values from JSON
Echo $jsonstrong | JQ '. Retmsg' ා get retmsg returned
Echo $jsonstr | JQ 'retdata' | JQ '. Phone' ා get the returned phone
Iv. Source code example
/bin/bash!
#author:zhengsc
#Description: query service
#Query mobile phone number location
#Variable definition
APIKEY=7531365f3dc9873af649bce596aacb92 #apikey
Rest api_phone = (http://apis.baidu.com/apistore/mobilenumber/mobilenumber? Phone = ') call the API's query address
Phonepattern = '^ 1 [34578] [0-9] {9} $' (mobile number regular expression verification)
EXIT='^[a-z|A-Z]*$'
Menu
ShowMenu () {
Read -p "
*****************************************
* mobile number query tool *
* author:zhengsc *
* date:2016-09-23 *
* exit: a-z *
*****************************************
Please enter your mobile number: "phone
}
#Call interface query
#Format of JSON result return
To be surprised
# "errNum": 0,
# "retMsg": "success",
# "retData": {
# "phone": "15270864042",
# "prefix": "1527086",
#"Supplier": "move",
#"Province": "Jiangxi",
#"City": "Nanchang",
#"Submit": "152 cards"
I am sorry.
To be confused
phoneQuery(){
json=$(curl -s --get "$RESTAPI_PHONE${phone}" -H "apikey:${APIKEY}")
#echo "${json}" | jq '.'
parseJson ${json}
}
#json explanation
ParseJson () {
JsonStr=$1
Retmsg = $(echo ${jsonstr} JQ '. Retmsg') (return code)
if [ "${retMsg}" = '"success"' ]; then
Province = $(echo ${jsonstr} JQ '. Retdata' | JQ '. Province')
City = $(echo ${jsonstr} JQ '. Retdata' | JQ '. City')
Supplier = $(echo ${jsonstr} JQ '. Retdata' | JQ '. Supplier') ාoperator
Submit = $(echo ${jsonstr} JQ '. Retdata' | JQ '. Suit') ා phone card type
Echo "home query result: ${province} ${City} ${supplier} ${suit}"
Else
Echo "query failed!"
Fi
}
Installjq () {
if [[ ! `which jq` ]]; then
Echo "your system does not have the JQ tool installed. Please install the JQ tool first!"
# adaptive for CentOS or Ubuntu
apt-get -qq -y install jq || yum -y install jq
[$? - Ne 0] & & echo "JQ tool installation failed!"; echo "JQ tool installation succeeded!"
Echo "$(which JQ) found!"
Fi
}
Main function
Main () {
Installjq - install JQ tools
Showmenu calls the menu function
if [[ "${phone}" =~ $EXIT ]]; then
Echo "you have exited the query tool, thank you for using"
Exit - press any letter key to exit the query tool
Fi
if [[ "${phone}" =~ $PHONEPATTERN ]]; then
phoneQuery ${phone}
Else
Echo "the mobile number you entered is not in the correct format"
ShowMenu
Fi
}
#Main function call
Main
v. Running screenshots
Write a picture description here