PHP通過POST提交XML/JSON數(shù)據(jù)

學(xué)習(xí)筆記

/**
 * [postParams post提交數(shù)據(jù)]
 * @param  [type]  $url     [接口地址]
 * @param  [type]  $params  [參數(shù)]
 * @param  boolean $useCert [是否需要證書,默認(rèn)不需要]
 * @param  integer $second  [url執(zhí)行超時(shí)時(shí)間,默認(rèn)30s]
 * @param  boolean $json    [是否json]
 * @return [type]           [description]
 */
function postParams($url,$params, $useCert = false, $second = 30,$json=false)
{   
    $ch = curl_init();
    //設(shè)置超時(shí)
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);

    //如果有配置代理這里就設(shè)置代理
    // if($this->config['curlProxyHost'] != "0.0.0.0"
    //     && $this->config['curlProxyPort'] != 0){
    //     curl_setopt($ch,CURLOPT_PROXY, $this->config['curlProxyHost']);
    //     curl_setopt($ch,CURLOPT_PROXYPORT, $this->config['curlProxyPort']);
    // }
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);//2 嚴(yán)格校驗(yàn) ,不想驗(yàn)證也可設(shè)為0
    //設(shè)置header
    // $header[] = "Content-type: text/xml";
    // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    // curl_setopt($ch, CURLOPT_HEADER, "Content-type: text/xml");
    //要求結(jié)果為字符串且輸出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    if($useCert == true){
        curl_setopt($ch, CURLOPT_VERBOSE, '1'); //debug模式,方便出錯(cuò)調(diào)試
        //設(shè)置證書
        //使用證書:cert 與 key 分別屬于兩個(gè).pem文件
        curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLCERT,  dirname(dirname(dirname(__FILE__))) . '/' . config('sslCertPath'));
        //curl_setopt($ch, CURLOPT_SSLCERTPASSWD,"Abcd1234");
        curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
        curl_setopt($ch,CURLOPT_SSLKEY, dirname(dirname(dirname(__FILE__))) . '/' . config('sslKeyPath'));
        //curl_setopt($ch, CURLOPT_SSLKEYPASSWD,"Abcd1234");
    }
    if($json && is_array($params)) {
        $params = json_encode($params);
    }
    //設(shè)置post方式提交
    curl_setopt($ch, CURLOPT_POST, 1);
    //設(shè)置post數(shù)據(jù)
    if($json){ //發(fā)送JSON數(shù)據(jù)
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array('Content-Type: application/json; charset=utf-8',
                'Content-Length:' . strlen($params)
            )
        );
    }else{
        // var_dump(json_decode($params));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(json_decode($params)));
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            array('Content-Type: application/x-www-form-urlencoded; charset=utf-8'));
    }
    //運(yùn)行curl
    $data = curl_exec($ch);
    //返回結(jié)果
    if($data){
        curl_close($ch);
        // DI()->logger->debug('curl reback data',$data);
        return $data;
    } else {
        $error = curl_errno($ch);
        curl_close($ch);
        // DI()->logger->error("curl出錯(cuò)","錯(cuò)誤碼:$error");
        return false;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容