A short example of PHP creating interfaces and calling interfaces (local)

Source: Internet
Author: User

**********************************************************************************************/*This is a small interface created by PHP*/<?PHP$mysqli=NewMysqli (' localhost ', ' root ', ' 123456 ', ' interfacetest '));Mysqli_query($mysqli, "Set names UTF8");if($mysqli-connect_error) {     die(' Connect Error ('.$mysqli->connect_errno. ‘) ‘ .$mysqli-connect_error);}$output=Array();$user _by=$_post[' User_by '];$uid=$_post["UID"];if(Empty($user _by)) {    $output=Array(' Data ' =NULL, ' info ' = ' This is null! ', ' Stats ' =>1); Exit(Json_encode ($output));}if($user _by= = ' Get_userinfo ') {//call the interface to get user information//query database    $sql= "SELECT *" From user WHERE user_id=$uid"; $result=$mysqli->query ($sql); $userInfo=$result-Fetch_row (); if($userInfo){//If the data has output data        $output=Array(            ' Data ' =Array(                ' UserInfo ' =$userInfo,            ), ' Stats ' =>0        ); }Else{        $output=Array(            ' Data ' =Array(                ' UserInfo ' =$userInfo,            ), ' Stats ' =>1        ); }    Exit(Json_encode ($output));//feedback The results to the client} $mysqli-close ();? >**********************************************************************************/*This is a method of an interface call (POST)*/$url= ' http://localhost/testmysql.php ';$SL _data=Array(    ' UID ' =>1, ' user_by ' = ' get_userinfo ');$ch=curl_init (); curl_setopt ($ch, Curlopt_url,$url);//the address to be accessedcurl_setopt ($ch, Curlopt_returntransfer, 0);//whether the execution result is returned, 0 is returned, 1 is not returned as 1 for transmitting data, and 0 for direct output displaycurl_setopt ($ch, Curlopt_post, 1);//send a regular POST requestcurl_setopt ($ch, Curlopt_postfields,Http_build_query($SL _data));$output= Curl_exec ($ch);//execute and get dataEcho $output; Curl_close ($ch);

?>

A short example of PHP creating interfaces and calling interfaces (local)

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.