This article mainly shares with you the detailed explanation of PHP interface programming. This article is very detailed and I hope it can help everyone.
1. The thinkPHP framework is introduced in the project (not introduced in detail)
2. Interface data return processing process
1. Determine the url request address
2. If it is a POST request, you need to combine the $data parameters, which is the data that needs to be sent.
3. Send the request with the transfer parameters
4 .Return data must be processed
3. Use a professional send request tool library: curl
## curl usage steps: curl_init ($url) url initialization
# Curl_exec () Sending requests
# counter ## View the parameter settings of the pair through the PHP manual, and then use the encapsulated request method
## Step 2: Create a public method in function.php under the Conmmon module/Conmon folder, method name: request, use curl to request to send
##
<?phpfunction request($url,$https=true,$method='get',$data=null){ //1.初始化curl $ch = curl_init($url); //2.curl_setopt()设置参数 根据实际请求需求进行参数封装 curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。 //如果是https请求 if($https === true){ //FALSE 禁止 cURL 验证对等证书 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); } //如果是post请求 if($method ==='post'){ curl_setopt($ch,CURLOPT_POST,true); //TRUE 时会发送 POST 请求 curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//发送post的数据 } //3.curl_exec()发送请求 $result = curl_exec($ch); //4.curl_close关闭请求 curl_close($ch); return $result; }
#The effect is as follows:
related suggestion:
Detailed explanation of token in php interface
The above is the detailed content of Detailed explanation of php interface programming. For more information, please follow other related articles on the PHP Chinese website!