直接上代码:
1 /** 2 * 以post方式提交xml到对应的接口url 3 * 4 * @param string $xml 需要post的xml数据 5 * @param string $url url 6 * @param bool $useCert 是否需要证书,默认不需要 7 * @param int $second url执行超时时间,默认30s 8 * @throws WxPayException 9 */ 10 private static function postXmlCurl($xml, $url, $useCert = false, $second = 30) 11 { 12 $ch = curl_init(); 13 //设置超时 14 curl_setopt($ch, CURLOPT_TIMEOUT, $second); 15 16 //如果有配置代理这里就设置代理 17 if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0" 18 && WxPayConfig::CURL_PROXY_PORT != 0){ 19 curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST); 20 curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT); 21 } 22 curl_setopt($ch,CURLOPT_URL, $url); 23 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); 24 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 25 //设置header 26 curl_setopt($ch, CURLOPT_HEADER, FALSE); 27 //要求结果为字符串且输出到屏幕上 28 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 29 30 if($useCert == true){ 31 //设置证书 32 //使用证书:cert 与 key 分别属于两个.pem文件 33 curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM'); 34 curl_setopt($ch,CURLOPT_SSLCERT, WxPayConfig::SSLCERT_PATH); 35 curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM'); 36 curl_setopt($ch,CURLOPT_SSLKEY, WxPayConfig::SSLKEY_PATH); 37 } 38 //post提交方式 39 curl_setopt($ch, CURLOPT_POST, TRUE); 40 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 41 //运行curl 42 $data = curl_exec($ch); 43 //返回结果 44 if($data){ 45 curl_close($ch); 46 return $data; 47 } else { 48 $error = curl_errno($ch); 49 curl_close($ch); 50 throw new WxPayException("curl出错,错误码:$error"); 51 } 52 }
response.php
1 <?php 2 if(isset($_POST['name'])){ 3 if(!empty($_POST['name'])){ 4 echo '您好,',$_POST['name'].'!'; 5 } 6 } 7 ?>