Created
September 2, 2014 14:14
-
-
Save dhrrgn/0e56b19c0efc2459714f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = new Core\File\RemoteFile('http://bestclipartblog.com/clipart-pics/food-clip-art-6.png'); | |
// Check if exists: | |
var_dump($file->exists());// bool(true) | |
// Get HTTP Status Code | |
var_dump($file->getHttpStatus()); // int(200) | |
// Content Length (in bytes) | |
var_dump($file->getContentLength()); // int(79157) | |
// Get the file content as a string | |
$file->getContent(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Core\File; | |
use DomainException; | |
use RuntimeException; | |
/** | |
* Class RemoteFile | |
* @package Core\File | |
*/ | |
class RemoteFile | |
{ | |
/** | |
* @var string | |
*/ | |
protected $url; | |
/** | |
* @param string $url | |
*/ | |
public function __construct($url) | |
{ | |
$this->setUrl($url); | |
} | |
/** | |
* Gets the Url. | |
* @return string | |
*/ | |
public function getUrl() | |
{ | |
return $this->url; | |
} | |
/** | |
* Validates, then sets the Url. | |
* @param $url | |
* @return $this | |
*/ | |
public function setUrl($url) | |
{ | |
if (! filter_var($url, FILTER_VALIDATE_URL)) { | |
throw new DomainException('Invalid Url: '.$url); | |
} | |
$this->url = $url; | |
return $this; | |
} | |
/** | |
* Checks if the remote file exists. | |
* @return bool | |
*/ | |
public function exists() | |
{ | |
$statusCode = $this->getHttpStatus(); | |
return $statusCode >= 200 && $statusCode < 400; | |
} | |
/** | |
* Gets the HTTP Status Code of the Url. | |
* @return int | |
*/ | |
public function getHttpStatus() | |
{ | |
$ch = $this->getCurl(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_NOBODY, true); | |
curl_exec($ch); | |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return (int) $status; | |
} | |
/** | |
* Gets the content length of the Url (in bytes). Returns -1 on failure. | |
* @return int | |
*/ | |
public function getContentLength() | |
{ | |
$fileSize = -1; | |
$ch = $this->getCurl(); | |
// Issue a HEAD request. | |
curl_setopt($ch, CURLOPT_NOBODY, 1); | |
curl_setopt($ch, CURLOPT_HEADER, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
$response = curl_exec($ch); | |
if ($response === false) { | |
throw new RuntimeException(curl_error($ch)); | |
} | |
curl_close($ch); | |
$contentLength = -1; | |
$status = -1; | |
if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $response, $matches)) { | |
$status = (int) $matches[1]; | |
} | |
if (preg_match("/Content-Length: (\d+)/", $response, $matches)) { | |
$contentLength = (int) $matches[1]; | |
} | |
if ($status == 200 || ($status > 300 && $status <= 308)) { | |
$fileSize = $contentLength; | |
} | |
return $fileSize; | |
} | |
/** | |
* Get the contents of the URL. | |
* @param array $curlOptions | |
* @return string | |
*/ | |
public function getContents($curlOptions = []) | |
{ | |
$ch = $this->getCurl(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); | |
// Curl Option overrides | |
foreach ($curlOptions as $option => $value) { | |
curl_setopt($ch, $option, $value); | |
} | |
$response = curl_exec($ch); | |
if (curl_errno($ch)) { | |
throw new DomainException(curl_error($ch)); | |
} | |
curl_close($ch); | |
return $response; | |
} | |
/** | |
* Gets the Curl Handle | |
* @return resource | |
*/ | |
protected function getCurl() | |
{ | |
return curl_init($this->getUrl()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment