php curl Content-Type: application/x-www-form-urlencoded

  • Post author:
  • Post category:php

public function test()
    {
        $url = 'xxx'; //请求域名
        $method = 'POST';

        $params = [
            'inputCharset'=>'UTF-8',
            'signType'=>'RSA',
            'sign'=>'xxx',//签名
            'payIp'=>'127.0.0.1',
            'returnUrl'=>'xxx', //地址
            'notifyUrl'=>'xxx', //回调地址
            'deviceType'=>'WEB',
            'payType'=>'CARDBANK',
            'merchantId'=>4613,
            'merchantTradeId'=>'4245368915748504',
            'currency'=>'JPY',
            'amountFee'=>'1',
            'goodsTitle'=>'remare_by_xh',
            'issuingBank'=>'UNIONPAY',
            // 'subIssuingBank'=>'123456',
            'version'=>'1.0',
        ];

        $p='';
        foreach ($params as $k => $v) {
            $p .= $k.'='.$v.'&';
        }
        $data = rtrim($p, '&');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        // 设置User-Agent
        // curl_setopt($ch, CURLOPT_USERAGENT, self::USER_AGENT);
        // 连接建立最长耗时
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        // 请求最长耗时
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        // 设置SSL版本
        curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // 如果报证书相关失败,可以考虑取消注释掉该行,强制指定证书版本
        //curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
        // 设置Basic认证
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        // curl_setopt($ch, CURLOPT_USERPWD, $this->appKey . ":" . $this->masterSecret);
        // 设置Post参数
        if ($method === 'POST') {
            curl_setopt($ch, CURLOPT_POST, true);
        } else if ($method === 'DELETE' || $method === 'HTTP_PUT') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        }

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


        // 设置headers
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            // 'Content-Type: application/json',
            'Content-Type: application/x-www-form-urlencoded',
            'Connection: Keep-Alive',
        ));


        // 执行请求
        $output = curl_exec($ch);
        // 解析Response
        $response = array();
        $errorCode = curl_errno($ch);

        if ($errorCode) {
            var_dump(123);
           var_dump($errorCode);

        } else {
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
            $header_text = substr($output, 0, $header_size);
            $body = substr($output, $header_size);
            $headers = array();
            foreach (explode("\r\n", $header_text) as $i => $line) {
                if (!empty($line)) {
                    if ($i === 0) {
                        $headers['http_code'] = $line;
                    } else if (strpos($line, ": ")) {
                        list ($key, $value) = explode(': ', $line);
                        $headers[$key] = $value;
                    }
                }
            }
            $response['headers'] = $headers;
            $response['body'] = $body;
            $response['http_code'] = $httpCode;
        }
        curl_close($ch);

        var_dump($body);
        die;
        return $response;
    }

版权声明:本文为taotie_原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。