php 获取远程数据

180it 2021-02-28 PM 1352℃ 0条
/**
 * 获取远程数据
 * @param  $url 地址
 * @param  $post_data 参数
 * @param  $method 方法
 * @param  $timeout 延时
 */
function send($url = "", $post_data = '', $method = 'POST', $timeout = 20) {
    if ($method == 'GET'){
        if(is_array($post_data)){
            $post_data = implode("&",$post_data);
        } 
        $url = $url.'?'.ltrim($post_data, '?');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*'));
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if ($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
    $content = curl_exec($ch);
    $response = curl_getinfo($ch);
    return $content;
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php 获取远程数据