|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Helper for calls remote components/local services |
| 4 | + * @author Alex Yatsenko |
| 5 | + * @link https://github.com/yatsenkolesh/php-curlasync |
| 6 | + * @license BSD-3-Clause |
| 7 | +*/ |
| 8 | + |
| 9 | +namespace CurlAsync; |
| 10 | + |
| 11 | +class Talk |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var string $response |
| 15 | + */ |
| 16 | + public $response = null; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var string $requestUrl |
| 20 | + */ |
| 21 | + private $requestUrl = null; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array $postFields |
| 25 | + */ |
| 26 | + private $postFields = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var resource $curl |
| 30 | + */ |
| 31 | + private $curl = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string $url |
| 35 | + */ |
| 36 | + private $url; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var boolean $async |
| 40 | + */ |
| 41 | + private $async = false; |
| 42 | + |
| 43 | + /** |
| 44 | + * @param string $url |
| 45 | + * @return object Talk |
| 46 | + */ |
| 47 | + public static |
| 48 | + |
| 49 | + function instance($host = null) |
| 50 | + { |
| 51 | + return new self($host); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param string $url |
| 56 | + * @return |
| 57 | + */ |
| 58 | + public |
| 59 | + |
| 60 | + function __construct($host = null) |
| 61 | + { |
| 62 | + $this->host = $host; |
| 63 | + return ; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param array $post |
| 68 | + * @return object Talk |
| 69 | + */ |
| 70 | + public |
| 71 | + |
| 72 | + function setPost($post = []) |
| 73 | + { |
| 74 | + $this->postFields = $post; |
| 75 | + return $this; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Don't return result if you will be use request aync method |
| 80 | + * Make a current request is async |
| 81 | + * @return object Talk |
| 82 | + */ |
| 83 | + public |
| 84 | + |
| 85 | + function async() |
| 86 | + { |
| 87 | + $this->async = true; |
| 88 | + return $this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Make a current request is not async |
| 93 | + * @return object Talk |
| 94 | + */ |
| 95 | + public |
| 96 | + |
| 97 | + function await() |
| 98 | + { |
| 99 | + $this->async = false; |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @param string $url |
| 105 | + * @return object Talk |
| 106 | + */ |
| 107 | + public |
| 108 | + |
| 109 | + function request($url = null) |
| 110 | + { |
| 111 | + $this->curl = curl_init(); |
| 112 | + |
| 113 | + curl_setopt($this->curl, CURLOPT_URL, $this->host. $url); |
| 114 | + |
| 115 | + if(sizeof($this->postFields)) |
| 116 | + { |
| 117 | + curl_setopt($this->curl, CURLOPT_POST, 1); |
| 118 | + curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postFields); |
| 119 | + } |
| 120 | + else |
| 121 | + curl_setopt($this->curl, CURLOPT_HTTPGET, 1); |
| 122 | + |
| 123 | + |
| 124 | + curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
| 125 | + |
| 126 | + if($this->async) |
| 127 | + { |
| 128 | + curl_setopt($this->curl, CURLOPT_FRESH_CONNECT, true); |
| 129 | + |
| 130 | + //fucking curl |
| 131 | + if(explode('.', php_version())[0] == 7) |
| 132 | + curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, 50); |
| 133 | + else |
| 134 | + curl_setopt($this->curl, CURLOPT_TIMEOUT, 1); |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + $this->response = curl_exec($this->curl); |
| 139 | + |
| 140 | + curl_close ($this->curl); |
| 141 | + |
| 142 | + return $this; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @param string $url |
| 147 | + * @return string |
| 148 | + */ |
| 149 | + public static |
| 150 | + |
| 151 | + function simple($url = null) |
| 152 | + { |
| 153 | + $curl = curl_init($url); |
| 154 | + |
| 155 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
| 156 | + curl_setopt($curl, CURLOPT_VERBOSE, 1); |
| 157 | + curl_setopt($curl, CURLOPT_HEADER, 1); |
| 158 | + |
| 159 | + $response = curl_exec($curl); |
| 160 | + |
| 161 | + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
| 162 | + |
| 163 | + return |
| 164 | + [ |
| 165 | + 'body' => substr($response, $headerSize), |
| 166 | + 'header' => substr($response, 0, $headerSize), |
| 167 | + ]; |
| 168 | + } |
| 169 | +} |
| 170 | +?> |
0 commit comments