zoukankan      html  css  js  c++  java
  • 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;
        }
  • 相关阅读:
    关于JDK中的设计模式
    关于Java中的构造方法和set方法()给属性赋值
    关于Object[]数组强转成Integer[]类型的数组.
    [ImportNew]Java中的Timer类和TimerTask类
    关于Linux最基本总结
    关于数组
    关于Linux系统和Windows系统中文件夹的命名规范
    关于数组集合之间的转换
    关于Java获取系统信息
    关于Windows常用命令
  • 原文地址:https://www.cnblogs.com/lh460795/p/6655143.html
Copyright © 2011-2022 走看看