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;
}

                                                              
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容