<?php
function dcurl($url, $method = 'GET', $postFields = null, $header = null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);

    if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https")
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }

    switch ($method)
    {
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, true);
            if (!empty($postFields))
            {
                if (is_array($postFields) || is_object($postFields))
                {
                    if (is_object($postFields))
                        $postFields = Tools::object2array($postFields);
                    $postBodyString = "";
                    $postMultipart = false;
                    foreach ($postFields as $k => $v)
                    {
                        if ("@" != substr($v, 0, 1))
                        { //判断是不是文件上传
                            $postBodyString .= "$k=" . urlencode($v) . "&";
                        }
                        else
                        { //文件上传用multipart/form-data,否则用www-form-urlencoded
                            $postMultipart = true;
                        }
                    }
                    unset($k, $v);
                    if ($postMultipart)
                    {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
                    }
                    else
                    {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
                    }
                }
                else
                {
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
                }

            }
            break;
        default:
            if (!empty($postFields) && is_array($postFields))
                $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($postFields);
            break;
    }
}
?>

  

;

最新文章

  1. canvas简介
  2. Guest Speaker on 2015 WinHEC Shenzhen 秋季大会
  3. EditPlus 3.7.1186 中文版(10月27日更新)重大性能改进,推荐更新!
  4. RabbitMQ(四)
  5. FormatMessage与GetLastError配合使用,排查windows api调用过程中的错误
  6. swagger:The World&#39;s Most Popular Framework for APIs.
  7. 【CentOS】IBM X3650M4 IMM远程管理【转载】
  8. C#基础原理拾遗——面试都爱问的委托和事件(纠正)
  9. visual studio 2015 删除空行 ,缩进css
  10. 【Hibernate】Illegal attempt to associate a collection with two open sessions
  11. android在假设绘制自己定义的bitmap,然后返回给ImageView
  12. cocos2d-x-3.1 经常使用宏 (coco2d-x 学习笔记五)
  13. hdu--5351--MZL&#39;s Border
  14. TP-Link路由器的设置向导
  15. 201521123009 《Java程序设计》第12周学习总结
  16. 五十九、linux 编程—— I/O 多路复用 fcntl
  17. SSM结构
  18. JDK下载与安装、 Eclipse下载与使用、 Tomcat下载与使用、 MySQL安装与使用
  19. EOF多行写入文件防止变量替换
  20. ionic_ Network connectivity error occurred, are you offline?

热门文章

  1. 【Python3 爬虫】11_报错No module named 'requests'
  2. 【BIEE】16_饼图应用
  3. LoadRunner中对图表的分析说明
  4. 使用python-nmap 搭建基本端口扫描器
  5. python(30)- 常用模块
  6. sqlmap 定义别名冲突
  7. wamp通过phpMyAdmin修改登录密码
  8. 61. Search for a Range【medium】
  9. 60. Search Insert Position 【easy】
  10. vm中安装ubuntu16