<?php
/**
 * Created by PhpStorm.
 * User: hasee
 * Date: 2017/4/17
 * Time: 16:33
 */
function curl_post($url,$data=array(),$type="",$timeout=10,$header=array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    if($timeout>0)   curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);
    if(strstr($url,"https://")){
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    }
    $user_agent = "ZhuShangDai Curl/1.0";
    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
    if(!empty($data)) {
        $querystring = "";
        if (is_array($data)){
            $querystring = http_build_query($data);
        } else {
            $querystring = $data;
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $querystring);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    $header=$header?$header:array();
    if($type=="json"){
        $header[]="Content-Type: application/json; charset=utf-8";
        $header[]="Cache-Control: no-cache";
    }
    if($type=="json"){
        $header[]="Content-Type: application/json; charset=utf-8";
        $header[]="Cache-Control: no-cache";
    }elseif($type=="xml"){
        $header[]="Content-Type: text/xml; charset=utf-8";
    }

    if(!empty($header)){
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
    }

    $result=curl_exec($ch);
    $error = curl_error($ch);
    curl_close ($ch);
    return $error ? $error : $result;
}