ZapMachine/utils/ZAPCurl.class.php

64 lines
1.5 KiB
PHP
Raw Normal View History

<?php
class ZAPCurl {
private $url;
private $binary;
private $referer;
private $headers = array();
public $error = null;
public $httpCode = null;
//private $proxyIp = '127.0.0.1:81';
public function __construct($url, $binary = false) {
$this->url = $url;
$this->binary = $binary;
}
/**
* Set a custom header for the request
* @param string $key Header name
* @param string $value Header value
*/
public function setHeader($key, $value) {
$this->headers[] = $key . ': ' . $value;
}
/**
* Curl wrapper
* @param string $url
* @param boolean $binary
* @return mixed $curlData
*/
public function fetchCurlData() {
$cUrl = curl_init();
curl_setopt($cUrl, CURLOPT_URL, $this->url);
curl_setopt($cUrl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cUrl, CURLOPT_TIMEOUT, ZAP_CURL_TIMEOUT);
if (isset($this->referer)) {
curl_setopt($cUrl, CURLOPT_REFERER, $this->referer);
}
if (!empty($this->headers)) {
curl_setopt($cUrl, CURLOPT_HTTPHEADER, $this->headers);
}
//curl_setopt($cUrl, CURLOPT_PROXY, $this->proxyIp);
$curlData = curl_exec($cUrl);
$this->error = curl_error($cUrl);
$this->httpCode = curl_getinfo($cUrl, CURLINFO_HTTP_CODE);
// curl_close() is deprecated as of PHP 8.5 (no effect since PHP 8.0)
if ($this->error) {
error_log('ZAPCurl error: ' . $this->error . ' URL: ' . $this->url);
} elseif ($this->httpCode >= 400) {
error_log('ZAPCurl HTTP ' . $this->httpCode . ' URL: ' . $this->url);
}
return $curlData;
}
}
?>