-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlCurl.class.php
More file actions
60 lines (51 loc) · 1.78 KB
/
Copy pathXmlCurl.class.php
File metadata and controls
60 lines (51 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace WeChatPay;
class XmlCurl {
public $proxy;
public $second;
public $cert;
public function __construct($cert=[],$proxy=[],$second=30){
$this->cert = $cert;
$this->proxy = $proxy;
$this->second = $second;
}
public function post($xml, $url){
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $this->second);
//如果有配置代理这里就设置代理
if(!empty($proxy)){
curl_setopt($ch,CURLOPT_PROXY, $this->proxy['host']);
curl_setopt($ch,CURLOPT_PROXYPORT,$this->proxy['port']);
}
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if(!empty($this->cert)){
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $this->cert['sslcert']);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $this->cert['sslkey']);
}
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new \Exception("curl出错,错误码:$error");
}
}
}