DEV Community

leichengde
leichengde

Posted on

PHP request interface API: a relatively complete example (POST)

1:Send via curl
[Scenario: Get table information in remote server based on id]

<?php
//interface url
$url = '';
//data field
    $data = 'id=35289';
    // method to generate http header information
function getAuthHeaders(){
//appid needs to apply in advance
$appid = '';
$time = time();
//appkey needs to apply in advance
$appkey = '';
//Signature here we use the md5 of the following three
$sign = md5($appid . '&' . $time . $appkey);
return array(
            'Content-Type : application/json',
            'charset : '.'utf-8',
            //We also send the appid, TimeStamp, and signature to the server,
            //Request it to query whether the md5 signature of appkey authentication is correct or not through appid
'X-Auth-Appid : ' . $appid,
'X-Auth-TimeStamp : ' . $time,
'x-Auth-Sign : ' . $sign
        );
}
$ch = curl_init();
//address to visit
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, getAuthHeaders());
//Whether the execution result is returned, 0 is to return, 1 is not to return
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
// Send a regular POST request
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// execute and get data
$output = curl_exec($ch);
print_r($output);
    curl_close($ch);
?>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)