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);
?>

PHP is using the following to request other website data information

function http_post($url, $post_data, $header = [], $proxy = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (!empty($header)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    if (!empty($proxy)) {
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
        curl_setopt($ch, CURLOPT_PROXY, "{$proxy['ip']}:{$proxy['port']}");
        curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$proxy['username']}:{$proxy['password']}");
    }
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

Leave a Reply

Your email address will not be published.

en_USEnglish