PHP curl post/get 请求

/**                                                              
 * curl 模拟post请求                                             
 *                                                               
 * @param string $url  请求地址                                  
 * @param array $data  需要post的数据                            
 * @param int $timeout 超时时间(秒)                              
 * @return mixed                                                 
 */                                                              
function post($url, $data = [], $timeout = 3)                    
{                                                                
    $ch = curl_init();                                           
    curl_setopt($ch, CURLOPT_URL, $url);                         
    curl_setopt($ch, CURLOPT_POST, 1);                           
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置超时     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);             
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);             
    $result = curl_exec($ch);                                    
    curl_close($ch);                                             
    return $result;                                              
}   

/**
 * curl 模拟get请求
 *
 * @param string $url   请求地址
 * @param int $timeout  超时时间(秒)
 * @return mixed
 */
function get($url, $timeout = 3)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 设置超时
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

                                                              

你可能感兴趣的:(PHP curl post/get 请求)