• curl


        private function send_request($url) {
    
            if (function_exists('curl_exec')) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
                curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $curl_errno = curl_errno($ch);
                $data = curl_exec($ch);
                curl_close($ch);
                if ($curl_errno >0) {
                    return 0;
                }else{
                    return $data;
                }
            } else {
                $opts    = array(
                    'http' => array(
                        'method'  => "GET",
                        'timeout' => self::$connectTimeout + self::$socketTimeout,
                    )
                );
                $context = stream_context_create($opts);
                $data    = @file_get_contents($url, false, $context);
                if($data){ 
                    return $data;
                }else{ 
                    return 0;
                } 
            }
        }
        private function post_request($url, $postdata = '') {
            if (!$postdata) {
                return false;
            }
    
            $data = http_build_query($postdata);
            if (function_exists('curl_exec')) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout);
                curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
    
                //不可能执行到的代码
                if (!$postdata) {
                    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
                } else {
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                }
                $data = curl_exec($ch);
    
                if (curl_errno($ch)) {
                    $err = sprintf("curl[%s] error[%s]", $url, curl_errno($ch) . ':' . curl_error($ch));
                    $this->triggerError($err);
                }
    
                curl_close($ch);
            } else {
                if ($postdata) {
                    $opts    = array(
                        'http' => array(
                            'method'  => 'POST',
                            'header'  => "Content-type: application/x-www-form-urlencoded
    " . "Content-Length: " . strlen($data) . "
    ",
                            'content' => $data,
                            'timeout' => self::$connectTimeout + self::$socketTimeout
                        )
                    );
                    $context = stream_context_create($opts);
                    $data    = file_get_contents($url, false, $context);
                }
            }
    
            return $data;
        }
  • 相关阅读:
    jsADS的库(待更新)
    javascript设计模式工厂方法模式
    jQuery星级评价
    邮政编码联动地址
    ADS图像缩放与剪裁(只是完成了前台的功能,传送数据到后台的功能待完成)
    ie6png透明解决(ietester的ie6有问题,原生ie6是没问题的)
    javascript设计模式桥接模式
    每一个人都应该学会执着
    防止电脑被攻击
    获取用户的IP地址的三个属性的区别
  • 原文地址:https://www.cnblogs.com/lh460795/p/6655143.html
Copyright © 2020-2023  润新知