Skip to content

Commit

Permalink
more change
Browse files Browse the repository at this point in the history
  • Loading branch information
KhristenkoYura committed Mar 12, 2015
1 parent da954d5 commit 3bd82d8
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 257 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Example
=======
```php
// Create
use multiCurl\Client;
use MCurl\Client;

$client = new Client();

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "network/multi-curl",
"description": "Multi curl client for PHP 5.3",
"keywords": ["curl","multi", "client"],
"homepage": "http://github.com/khristenkoyura/multicurl",
"name": "khr/php-mcurl-client",
"description": "wrap curl client for PHP 5.3; using multi curl, parallel request and write asynchronous code",
"keywords": ["php", "curl","multi", "client"],
"homepage": "http://github.com/khristenkoyura/mcurl",
"type": "library",
"license": "MIT",
"authors": [
Expand Down Expand Up @@ -32,6 +32,6 @@
"libraries/phpquery": "*"
},
"autoload": {
"psr-4": {"multiCurl\\": "src/"}
"psr-4": {"MCurl\\": "src/"}
}
}
36 changes: 36 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/facebook_sites_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

include __DIR__ . '/../vendor/autoload.php';

use multiCurl\Client;
use MCurl\Client;

$sites = array(
'google.com',
Expand Down
254 changes: 5 additions & 249 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace multiCurl;
namespace MCurl;

class Client {

Expand Down Expand Up @@ -59,7 +59,7 @@ class Client {
* Return result class
* @var int microsecond
*/
protected $classResult = '\\multiCurl\\Result';
protected $classResult = '\\MCurl\\Result';

/**
* Sleep script undo $this->sleepNext request
Expand Down Expand Up @@ -469,253 +469,9 @@ protected function getResourceId( $resource ) {
}

public function __destruct() {
curl_multi_close( $this->mh );
}
}


class Result {

/**
* @see get{Name}()
*
* @var mixed $id
* @var resource $ch
* @var array $info
* @var array $options
* @var int $httpCode
* @var string $body
* @var resource $bodyStream
* @var \stdClass|null $json
* @var array $headers
* @var array $params
* @var bool $hasError
* @var string $errorType
* @var string $error
* @var int $errorCode
*
*/

/**
* @var array
*/
protected $query;

/**
* @var array
*/
protected $rawHeaders;

public function __construct($query) {
$this->query = $query;
}

/**
* Return id in request
* @return null|mixed
*/
public function getId() {
return isset($this->query['id']) ? $this->query['id'] : null;
}

/**
* cURL session: curl_init()
* @return resource
*/
public function getCh() {
return $this->query['ch'];
}

/**
* @see curl_getinfo();
* @return mixed
*/
public function getInfo() {
return curl_getinfo($this->query['ch']);
}

/**
* Return curl option in request
* @return array
*/
public function getOptions() {
$opts = $this->query['opts'];
unset($opts[CURLOPT_FILE]);
if (isset($opts[CURLOPT_WRITEHEADER])) {
unset($opts[CURLOPT_WRITEHEADER]);
}
return $opts;
}
/**
* Result http code
* @see curl_getinfo($ch, CURLINFO_HTTP_CODE)
* @return int
*/
public function getHttpCode() {
return (int) curl_getinfo($this->query['ch'], CURLINFO_HTTP_CODE);
}

/**
* Example:
* $this->getHeaders() =>
* return [
* 'result' => 'HTTP/1.1 200 OK',
* 'content-type' => 'text/html',
* 'content-length' => '1024'
* ...
* ];
*
* Or $this->headers['content-type'] => return 'text/html' @see $this->__get()
* @return array
*/
public function getHeaders() {
if (!isset($this->rawHeaders) && isset($this->query['opts'][CURLOPT_WRITEHEADER])) {
rewind($this->query['opts'][CURLOPT_WRITEHEADER]);
$headersRaw = stream_get_contents($this->query['opts'][CURLOPT_WRITEHEADER]);
$headers = explode("\n", rtrim($headersRaw));
$this->rawHeaders['result'] = trim(array_shift($headers));

foreach ($headers AS $header) {
list($name, $value) = array_map('trim', explode(':', $header, 2));
$name = strtolower($name);
$this->rawHeaders[$name] = $value;
}
}
return $this->rawHeaders;
}

/**
* Result in request
* @return string
*/
public function getBody() {
if (isset($this->query['opts'][CURLOPT_FILE])) {
rewind($this->query['opts'][CURLOPT_FILE]);
return stream_get_contents($this->query['opts'][CURLOPT_FILE]);
} else {
return curl_multi_getcontent($this->query['ch']);
}
}

/**
*
* @return mixed
*/
public function getBodyStream() {
rewind($this->query['opts'][CURLOPT_FILE]);
return $this->query['opts'][CURLOPT_FILE];
}

/**
* @see json_decode
* @return mixed
*/
public function getJson() {
$args = func_get_args();
if (empty($args)) {
return @json_decode($this->getBody());
} else {
array_unshift($args, $this->getBody());
return @call_user_func_array('json_decode', $args);
}
}

/**
* return params request
* @return mixed
*/
public function getParams() {
return $this->query['params'];
}


/**
* Has error
* @param null|string $type use: network|http
* @return bool
*/
public function hasError($type = null) {
$errorType = $this->getErrorType();
return (isset($errorType) && ($errorType == $type || !isset($type)));
}

/**
* Return network if has curl error or http if http code >=400
* @return null|string return string: network|http or null if not error
*/
public function getErrorType() {
if (curl_error($this->query['ch'])) {
return 'network';
}

if ($this->getHttpCode() >= 400) {
return 'http';
}

return null;
}

/**
* Return message error
* @return null|string
*/
public function getError() {
$message = null;
switch($this->getErrorType()) {
case 'network':
$message = curl_error($this->query['ch']);
break;
case 'http':
$message = 'http error ' . $this->getHttpCode();
break;
}
return $message;
}

/**
* Return code error
* @return int|null
*/
public function getErrorCode() {
$number = null;
switch($this->getErrorType()) {
case 'network':
$number = (int) curl_errno($this->query['ch']);
break;
case 'http':
$number = $this->getHttpCode();
break;
}
return $number;
}

public function __toString() {
return $this->getBody();
}

/**
* Simple get result
* @Example: $this->id, $this->body, $this->error, $this->hasError, $this->headers['content-type'], ...
*
* @param $key
* @return null
*/
public function __get($key) {
$method = 'get' . $key;
return method_exists($this, $method) ? $this->$method() : null;
}

public function __destruct() {
if (isset($this->query['opts'][CURLOPT_FILE]) && is_resource($this->query['opts'][CURLOPT_FILE])) {
fclose($this->query['opts'][CURLOPT_FILE]);
}

if (isset($this->query['opts'][CURLOPT_WRITEHEADER]) && is_resource($this->query['opts'][CURLOPT_WRITEHEADER])) {
fclose($this->query['opts'][CURLOPT_WRITEHEADER]);
}

if (is_resource($this->query['ch'])) {
curl_close($this->query['ch']);
if (isset($this->sh)) {
curl_share_close($this->sh);
}
curl_multi_close( $this->mh );
}
}
Loading

0 comments on commit 3bd82d8

Please sign in to comment.