php异步发送post 请求方法

<?php 

//curl方式
function post( $url, $data, $http_opts = null ) {

    if (!isset($url) || empty($url)) {
        return array( 'code' => 400, 'message' => '缺少请求链接' );
    }
    if (!isset($data) || empty($data)) {
        return array( 'code' => 400 ,'message' => '缺少请求参数');
    }

        //解析链接,判断请求协议
    $parse_url_array = parse_url($url);

    $curl_handler = curl_init();

    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_HEADER	 => false,
        CURLOPT_USERAGENT => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36',
        CURLOPT_POST => TRUE,
        CURLOPT_POSTFIELDS => $data
        );
    if ($parse_url_array['scheme'] == 'https') {
        $options[CURLOPT_SSL_VERIFYPEER] = 0;
        $options[CURLOPT_SSL_VERIFYHOST] = 0;
        $options[CURLOPT_SSLVERSION] = 3;
    }

    if (is_array($http_opts)) {
        foreach ($http_opts as $key => $value){
            $options[$key] = $value;
        }
    }

    curl_setopt_array($curl_handler, $options);
    $curl_result = curl_exec($curl_handler);

    $curl_http_status = curl_getinfo($curl_handler,CURLINFO_HTTP_CODE);
    if ($curl_result == false) {
        $error = curl_error($curl_handler);
        curl_close($curl_handler);
        return array( 'code' => $curl_http_status, 'message' => $error);
    }
//        if ($curl_result == false) {
//            curl_close($curl_handler);
//            return array( 'code' => 503, 'message' => '系统错误');
//        }
    curl_close($curl_handler);

        /*
        $encode = mb_detect_encoding($curl_result, array('ASCII', 'UTF-8','GB2312', 'GBK', 'BIG5'));

        if ($encode != 'UTF-8') {
            $curl_result = iconv($encode, 'UTF-8', $curl_result);
        }*/

        /*$result = json_decode($curl_result, true);
        if (is_null($result)) {
            $result = $curl_result;
        }*/


        return array( 'code' => 200, 'message' => 'ok', 'data' => $curl_result );

    }

//简单curl方式
    function postTwo($url,$xmlData){

    $header[] = "Content-type: text/xml";        //定义content-type为xml,注意是数组
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
    $response = curl_exec($ch);
    if(curl_errno($ch)){
        print curl_error($ch);
    }
    curl_close($ch);
     return array( 'code' => 200, 'message' => 'ok', 'data' => $response );


}
//fsockopen方式
function postFsockopen($server_ip,$xmlData){


    $fp = fsockopen($server_ip, 80);
    fputs($fp, "POST $path HTTP/1.0\r\n");
    fputs($fp, "Host: $server\r\n");
    fputs($fp, "Content-Type: text/xml\r\n");
    fputs($fp, "Content-Length: $contentLength\r\n");
    fputs($fp, "Connection: close\r\n");
    fputs($fp, "\r\n"); // all headers sent
    fputs($fp, $xml_data);
    $result = '';
    while (!feof($fp)) {
        $result .= fgets($fp, 128);
    }
    return array( 'code' => 200, 'message' => 'ok', 'data' => $result );

}

//请求的报文数据,xml格式
$data = <<<XML
<?xml version="1.0" encoding="GBK"?>
<packet>
    <transName>KJQY</transName>
    <Plain>TranAbbr=KJQY|TranType=0|Merc_id=8011623|MercDtTm=20161221112908|CheckFlag=1|IdType=1|IdNo=522622198802200532|Account=6225212680156772|PayCardName=%C6%D6%B7%A21127002202|AccountType=1|MobileNo=13812345678|MercCode=910308160000601|MercUrl=|MerChannelId=1</Plain>
    <Signature>783ca4feb7af10648591cb80a4d645d81f9d5852d4fe556ca24bd41b657685fd2b9bebf6aee1dbe3d8cf10a7375b427fecec9ba8c9de2bbf4dac9f1315027e3d1785463c53db242623cb7c9fb97191780a551bd75635e028cf4c87eb9b77626046e0dbb619c0f7ce93e2b629cf5e9589cf583db4a30eddd8afdf045dda167895</Signature>
</packet>
XML;

//请求的url
$url ='http://124.74.239.32/payment/paygate';

/*$r = post($url,$data);
$xmlObj=simplexml_load_string($r['data']);
echo $xmlObj->ErrorCode;
echo $xmlObj->ErrorMsg;*/

#2ciceshi
/*$r = postTwo($url,$data);
print_r($r);die;
$xmlObj=simplexml_load_string($r['data']);
echo "postTwo:";
echo $xmlObj;
echo $xmlObj->ErrorCode;
echo $xmlObj->ErrorMsg;
*/

#3fsockopen(hostname)

/*$r = postFsockopen($url,$data);
$xmlObj=simplexml_load_string($r['data']);
echo "postFsockopen:";
echo $xmlObj->ErrorCode;
echo $xmlObj->ErrorMsg;*/







已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页