zoukankan      html  css  js  c++  java
  • PHP向服务器发送POST请求

    /**
     * 功能:PHP向服务器发送POST请求
     * @param string $url 要请求的url地址,必选
     * @param array $post 请求参数,可选
     * @param array $options curl配置参数,可选
     * @return mixed
     */
    function httpSendPost($url, $post = array(), $options = array()) {
        $defaults = array(
            CURLOPT_POST => 1,
            CURLOPT_HEADER => 0,
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_TIMEOUT => 5,
            CURLOPT_CONNECTTIMEOUT => 5,
            CURLOPT_POSTFIELDS => http_build_query($post, '', '&'),
            CURLOPT_REFERER => "http://www.baidu.com",
        );
        $ch = curl_init();
        curl_setopt_array($ch, ($options + $defaults));
        $result = curl_exec($ch);
        //调试信息
        if (defined('DEBUG') && true === DEBUG) {
            $info  = curl_getinfo($ch);
            $arr   = array();
            $arr[] = array('opt', 'info');
            $arr[] = array('query', $post ? var_export($post, true) : '');
            $arr[] = array('response', $result);
            foreach ($info as $k => $v) {
                $arr[] = array($k, $v);
            }
            var_dump($arr);die;
        }
        if (curl_error($ch)) {
            $result = false;
        }
        curl_close($ch);
        return $result;
    }
  • 相关阅读:
    UVA11367 Full Tank?
    不均衡样本集问题
    NLP interview
    Linux 指令
    Python 趣题
    Grid Illumination
    动态规划-Minimum Cost to Merge Stones
    Contest 141
    Python join()方法
    Single Number
  • 原文地址:https://www.cnblogs.com/yeshaoxiang/p/7844170.html
Copyright © 2011-2022 走看看