MENU service case
 Website construction website design Beijing website construction high-end website production company Shangpin China
We create by embracing change
360 ° brand value__
simplified Chinese character
Simplified Chinese English

Shangpin China Joins Hands with Beisheng Internet to Create a New Chapter in Website Construction

Type: Shangpin Dynamic Learn more

How to call the JSON interface and write the interface with PHP?

Source: Shangpin China | Type: website encyclopedia | Time: June 25, 2018
Data docking, website interface Such problems are often mentioned in the process of website development? The so-called website interface is the shared boundary of information exchange between two independent servers in the website system.
1. Sharing boundary between servers
Simply explained, a website is actually a server, Enterprise website production The news, data and information displayed are stored in their own servers.
But when two websites, that is, two servers, need to transmit data or share some data, they need a channel or a method to let two servers interact with each other.
The content in the figure is what we call the shared boundary, that is, the interface content (the interface is located in the A server displaying the content, providing access channels and methods)
There are only two basic server interface forms, httpservice and webservice, and the formats of data transmission include json, xml, vdf, etc
Encryption methods include sha1, sha256, md5, and base64 with key signature verification
 
2. Example interface with strong practicability
(1) Single sign on based on oauth2.0
When your website needs the login function of third-party accounts such as QQ and WeChat, how can you verify that this user has an account in QQ and WeChat?
First of all, QQ and WeChat will not send the user name and password to your website, which is very unsafe.
Therefore, both parties need to verify some common information, which is called signature.
For example, WeChat message processing.
Signature form
 
(2) Ajax cross domain data transmission of jquery
When two websites under your banner need to share an interconnected data, you need a way to access it (this data can be played publicly, and it is also information that needs to be published and accessible to other users). When you need to cross domains, you need to use server language (php, java, etc.). If you still insist on using javascript and feel that it has advantages in speed, please use jsonp (remember that the format of jsonp is different from that of json, and there is a function in front of it). The calling interface must have an interface address. We use the free online weather forecast interface just to learn how to use the interface, which is not guaranteed to be accurate. At present, accurate interfaces have been charged. The free address I found is: //www.weather.com.cn/data/sk/101010100.html But let's use this address to complete our study.

PHP calls the JSON interface in the following five steps:

Step 1: Declare the page code, UTF-8:
header("Content-type: text/html; charset=utf-8");
Step 2: return the json string through curl:
Get and return the json string through the curl function
Step 3: convert the returned string into a php variable:
Step 4: Convert the string into an array:
Use the array conversion function:
 
Call string conversion function.
The following part is the test output:
The foreground displays the results:
 
Step 5: Call array elements:
Get the corresponding information through this two-dimensional array.
Foreground display:
 
Summary:
From the above example, we can see that a simple interface is actually very convenient to call. It can be achieved through the above five steps. In the above example, there are two key points. The first is to call the crul function; The second is to convert the json string into an array.

The following describes how to write interfaces in PHP

1、 First, answer two simple questions:

1. Can PHP develop clients?

Answer: No, because PHP is a scripting language and is responsible for completing the S part of B/S architecture or C/S architecture, that is, the development of the server. (Don't bother with GTK and WinBinder)

2. Why choose PHP as the first choice for developing the server?

A: The perfect partner of cross platform (running under UNIX, Linux, Windows, Mac OS), low consumption (PHP consumes relatively little system resources), high efficiency (relatively speaking), and MySQL is free and open source.

2、 How to use the PHP development API (Application Programming Interface)?

Those who have done API should understand that API development is simpler than WEB development, but the logic may be more complex. Because API is actually data output and does not need to present pages, there is no MVC (API only has M and C),

1. As with Web development, some relevant parameters are needed first. These parameters will be transmitted from the client, either GET or POST. This needs to be agreed between the development teams or a unified specification.

2. With parameters, data processing can be completed according to application requirements, such as task progress update, APP internal purchase, data submission at the end of a game, etc.

3. After the data logic is processed, how to return the relevant data needed by the client, such as task status, internal purchase results, player information, etc. to the client? Direct output, such as JSON, XML, TEXT, etc.

4. After the client gets the data you returned, it interacts with the user locally on the client.

A simple API example written temporarily:

<? php

$output = array();

$a = @$_GET['a'] ? $_ GET['a'] : '';

$uid = @$_GET['uid'] ? $_ GET['uid'] : 0;

if (empty($a)) {

$output=array ('data '=>NULL,'info'=>'White Fox Website!', 'code'=>- 201);

exit(json_encode($output));

}

//Walking interface

if ($a == 'get_users') {

//Check User

if ($uid == 0) {

$output = array('data'=>NULL, 'info'=>'The uid is null!', 'code'=>-401);

exit(json_encode($output));

}

//Assume that $mysql is a database

$mysql = array(

10001 => array(

'uid'=>10001,

'vip'=>5,

'nickname' => 'wang',

'email'=>' [email protected] ',

'qq'=>418808288,

'gold'=>1500,

'powerplay'=> array('2xp'=>12,'gem'=>12,'bingo'=>5,'keys'=>5,'chest'=>8),

'gems'=> array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),

'ctime'=>13152859989,

'lastLogin'=>13810380242,

'level'=>19,

'exp'=>16758,

),

10002 => array(

'uid'=>10002,

'vip'=>50,

'nickname' => 'dalu',

'email'=>' [email protected] ',

'qq'=>NULL,

'gold'=>14320,

'powerplay'=> array('2xp'=>1,'gem'=>120,'bingo'=>51,'keys'=>5,'chest'=>8),

'gems'=> array('red'=>13,'green'=>3,'blue'=>8,'yellow'=>17),

'ctime'=>13520623857,

'lastLogin'=>15720366940,

'level'=>112,

'exp'=>167588,

),

10003 => array(

'uid' => 10003,

'vip' => 5,

'nickname' => 'hb0317',

'email' => ' [email protected] ',

'qq' => NULL,

'gold' => 1541,

'powerplay'=> array('2xp'=>2,'gem'=>112,'bingo'=>4,'keys'=>7,'chest'=>8),

'gems' => array('red'=>13,'green'=>3,'blue'=>9,'yellow'=>7),

'ctime' => 13693231513,

'lastLogin'=> 13910396104,

'level' => 10,

'exp' => 1758,

),

);

$uidArr = array(10001,10002,10003);

if (in_array($uid, $uidArr, true)) {

$output = array('data' => NULL, 'info'=>'The user does not exist!', 'code' => -402);

exit(json_encode($output));

}

//Query database

$userInfo = $mysql[$uid];

//Output Data

$output = array(

'data' => array(

'userInfo' => $userInfo,

'isLogin'=>true,//whether to log in for the first time

'unread'=>4,//The number of unread messages

'untask'=>3,//The task is not completed

),

'info'=>'Here is the message which, commonly used in the popup window',//The message prompts that the client often uses this as the pop-up information.

'code'=>200,//Success and failure codes are generally positive or negative

);

exit(json_encode($output));

} elseif ($a == 'get_games_result') {

//...

Die ('You are calling the get_games_result interface! ');

} elseif ($a == 'upload_avatars') {

//....

Die ('You are calling the upload_avatars interface! ');

92.}
 

Source Statement: This article is original or edited by Shangpin China's editors. If it needs to be reproduced, please indicate that it is from Shangpin China. The above contents (including pictures and words) are from the Internet. If there is any infringement, please contact us in time (010-60259772).
TAG label:

What if your website can increase the number of conversions and improve customer satisfaction?

Make an appointment with a professional consultant to communicate!

* Shangpin professional consultant will contact you as soon as possible

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]

Communicate with professional consultants now!

  • National Service Hotline

    400-700-4979

  • Beijing Service Hotline

    010-60259772

Please be assured to fill in the information protection
Online consultation

Disclaimer

Thank you very much for visiting our website. Please read all the terms of this statement carefully before you use this website.

1. Part of the content of this site comes from the network, and the copyright of some articles and pictures involved belongs to the original author. The reprint of this site is for everyone to learn and exchange, and should not be used for any commercial activities.

2. This website does not assume any form of loss or injury caused by users to themselves and others due to the use of these resources.

3. For issues not covered in this statement, please refer to relevant national laws and regulations. In case of conflict between this statement and national laws and regulations, the national laws and regulations shall prevail.

4. If it infringes your legitimate rights and interests, please contact us in time, and we will delete the relevant content at the first time!

Contact: 010-60259772
E-mail: [email protected]