78 private $method = null;
88 function __construct($url, $method =
'GET', $params = null, $header = null)
92 $this->method = strtoupper($method);
93 if (is_array($params)) {
94 $this->request = http_build_query($params);
96 $this->request = $params;
98 if (!empty($header)) {
99 $this->requestHeaders = explode(
"\n", $header);
114 if (function_exists(
'curl_init')) {
117 curl_setopt($ch, CURLOPT_URL, $this->url);
118 if (!empty($this->requestHeaders)) {
119 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
121 curl_setopt($ch, CURLOPT_HEADER, 0);
123 if ($this->method ===
'POST') {
124 curl_setopt($ch, CURLOPT_POST,
true);
125 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
126 }
else if ($this->method !==
'GET') {
127 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
128 if (!is_null($this->request)) {
129 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
132 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
133 curl_setopt($ch, CURLINFO_HEADER_OUT,
true);
134 curl_setopt($ch, CURLOPT_HEADER,
true);
135 curl_setopt($ch, CURLOPT_SSLVERSION,3);
136 $chResp = curl_exec($ch);
137 $this->ok = $chResp !==
false;
139 $chResp = str_replace(
"\r\n",
"\n", $chResp);
140 $chRespSplit = explode(
"\n\n", $chResp, 2);
141 if ((count($chRespSplit) > 1) && (substr($chRespSplit[1], 0, 5) ===
'HTTP/')) {
142 $chRespSplit = explode(
"\n\n", $chRespSplit[1], 2);
144 $this->responseHeaders = $chRespSplit[0];
145 $resp = $chRespSplit[1];
146 $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
147 $this->ok = $this->status < 400;
149 $this->error = curl_error($ch);
152 $this->requestHeaders = str_replace(
"\r\n",
"\n", curl_getinfo($ch, CURLINFO_HEADER_OUT));
154 $this->response = $resp;
157 $opts = array(
'method' => $this->method,
158 'content' => $this->request
160 if (!empty($this->requestHeaders)) {
164 $ctx = stream_context_create(array(
'http' => $opts));
165 $fp = @fopen($this->url,
'rb',
false, $ctx);
167 $resp = @stream_get_contents($fp);
168 $this->ok = $resp !==
false;
170 }
catch (\Exception $e) {
send()
Send the request to the target URL.
__construct($url, $method= 'GET', $params=null, $header=null)
Class constructor.
$status
Status of response (0 if undetermined).
$requestHeaders
Request headers.
$responseHeaders
Response headers.
$ok
True if message was sent successfully.
Class to represent an HTTP message.