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;
        }
  • 相关阅读:
    tomcat启动Publishing failed with multiple errors
    oracle中merge into用法解析
    ORACLE数据库导出表,字段名,长度,类型,字段注释,表注释语句
    orcale的to_number方法
    Git删除无效远程分支
    .net core文件系统简介
    PowerShell的一些资料整理
    Jetbrains推出了一款新的编程字体Mono
    .net core程序强制以管理员权限启动
    在Asp.net Razor Pages/MVC程序中集成Blazor
  • 原文地址:https://www.cnblogs.com/lh460795/p/6655143.html
Copyright © 2011-2022 走看看