JFIFxxC      C  " }!1AQa"q2#BR$3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz w!1AQaq"2B #3RbrPKZAunirest-php/composer.jsonnu[{ "name": "mashape/unirest-php", "description": "Unirest PHP", "keywords": ["rest", "curl", "http", "https", "client"], "type": "library", "homepage": "https://github.com/Mashape/unirest-php", "license": "MIT", "author": "Mashape (https://www.mashape.com)", "require": { "php": ">=5.4.0", "ext-curl": "*" }, "suggest": { "ext-json": "Allows using JSON Bodies for sending and parsing requests" }, "require-dev": { "phpunit/phpunit": "~4.4", "codeclimate/php-test-reporter": "0.1.*" }, "autoload": { "psr-0": { "Unirest\\": "src/" } }, "support": { "email": "opensource@mashape.com" } } PKZ7ʑ&unirest-php/tests/Unirest/BodyTest.phpnu[assertEquals($file, sprintf('@%s;filename=%s;type=', $fixture, basename($fixture))); } else { $this->assertTrue($file instanceof \CURLFile); } } public function testHttpBuildQueryWithCurlFile() { $fixture = __DIR__ . '/fixtures/upload.txt'; $file = Body::File($fixture); $body = array( 'to' => 'mail@mailinator.com', 'from' => 'mail@mailinator.com', 'file' => $file ); $result = Request::buildHTTPCurlQuery($body); $this->assertEquals($result['file'], $file); } public function testJson() { $body = Body::Json(array('foo', 'bar')); $this->assertEquals($body, '["foo","bar"]'); } public function testForm() { $body = Body::Form(array('foo' => 'bar', 'bar' => 'baz')); $this->assertEquals($body, 'foo=bar&bar=baz'); // try again with a string $body = Body::Form($body); $this->assertEquals($body, 'foo=bar&bar=baz'); } public function testMultipart() { $arr = array('foo' => 'bar', 'bar' => 'baz'); $body = Body::Multipart((object) $arr); $this->assertEquals($body, $arr); $body = Body::Multipart('flat'); $this->assertEquals($body, array('flat')); } public function testMultipartFiles() { $fixture = __DIR__ . '/fixtures/upload.txt'; $data = array('foo' => 'bar', 'bar' => 'baz'); $files = array('test' => $fixture); $body = Body::Multipart($data, $files); // echo $body; $this->assertEquals($body, array( 'foo' => 'bar', 'bar' => 'baz', 'test' => Body::File($fixture) )); } } PKZ_{|*unirest-php/tests/Unirest/ResponseTest.phpnu[assertEquals($response->body['a'], 1); } public function testJSONAObjects() { $opts = Request::jsonOpts(false); $response = new Response(200, '{"a":1,"b":2,"c":3,"d":4,"e":5}', '', $opts); $this->assertEquals($response->body->a, 1); } public function testJSONOpts() { $opts = Request::jsonOpts(false, 512, JSON_NUMERIC_CHECK); $response = new Response(200, '{"number": 1234567890}', '', $opts); $this->assertSame($response->body->number, 1234567890); } } PKZBB)unirest-php/tests/Unirest/RequestTest.phpnu[assertTrue(property_exists($response->body->cookies, 'foo')); Request::clearCurlOpts(); } /** * @expectedException \Unirest\Exception */ public function testTimeoutFail() { Request::timeout(1); Request::get('http://mockbin.com/delay/1000'); Request::timeout(null); // Cleaning timeout for the other tests } public function testDefaultHeaders() { $defaultHeaders = array( 'header1' => 'Hello', 'header2' => 'world' ); Request::defaultHeaders($defaultHeaders); $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertObjectHasAttribute('header1', $response->body->headers); $this->assertEquals('Hello', $response->body->headers->header1); $this->assertObjectHasAttribute('header2', $response->body->headers); $this->assertEquals('world', $response->body->headers->header2); $response = Request::get('http://mockbin.com/request', ['header1' => 'Custom value']); $this->assertEquals(200, $response->code); $this->assertObjectHasAttribute('header1', $response->body->headers); $this->assertEquals('Custom value', $response->body->headers->header1); Request::clearDefaultHeaders(); $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertObjectNotHasAttribute('header1', $response->body->headers); $this->assertObjectNotHasAttribute('header2', $response->body->headers); } public function testDefaultHeader() { Request::defaultHeader('Hello', 'custom'); $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertTrue(property_exists($response->body->headers, 'hello')); $this->assertEquals('custom', $response->body->headers->hello); Request::clearDefaultHeaders(); $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertFalse(property_exists($response->body->headers, 'hello')); } public function testSetMashapeKey() { Request::setMashapeKey('abcd'); $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertTrue(property_exists($response->body->headers, 'x-mashape-key')); $this->assertEquals('abcd', $response->body->headers->{'x-mashape-key'}); // send another request $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertTrue(property_exists($response->body->headers, 'x-mashape-key')); $this->assertEquals('abcd', $response->body->headers->{'x-mashape-key'}); Request::clearDefaultHeaders(); $response = Request::get('http://mockbin.com/request'); $this->assertEquals(200, $response->code); $this->assertFalse(property_exists($response->body->headers, 'x-mashape-key')); } public function testGzip() { $response = Request::get('http://mockbin.com/gzip/request'); $this->assertEquals('gzip', $response->headers['Content-Encoding']); } public function testBasicAuthenticationDeprecated() { $response = Request::get('http://mockbin.com/request', array(), array(), 'user', 'password'); $this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $response->body->headers->authorization); } public function testBasicAuthentication() { Request::auth('user', 'password'); $response = Request::get('http://mockbin.com/request'); $this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $response->body->headers->authorization); } public function testCustomHeaders() { $response = Request::get('http://mockbin.com/request', array( 'user-agent' => 'unirest-php', )); $this->assertEquals(200, $response->code); $this->assertEquals('unirest-php', $response->body->headers->{'user-agent'}); } // GET public function testGet() { $response = Request::get('http://mockbin.com/request?name=Mark', array( 'Accept' => 'application/json' ), array( 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('Mark', $response->body->queryString->name); $this->assertEquals('thefosk', $response->body->queryString->nick); } public function testGetMultidimensionalArray() { $response = Request::get('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'key' => 'value', 'items' => array( 'item1', 'item2' ) )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('value', $response->body->queryString->key); $this->assertEquals('item1', $response->body->queryString->items[0]); $this->assertEquals('item2', $response->body->queryString->items[1]); } public function testGetWithDots() { $response = Request::get('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'user.name' => 'Mark', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('Mark', $response->body->queryString->{'user.name'}); $this->assertEquals('thefosk', $response->body->queryString->nick); } public function testGetWithDotsAlt() { $response = Request::get('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'user.name' => 'Mark Bond', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('Mark Bond', $response->body->queryString->{'user.name'}); $this->assertEquals('thefosk', $response->body->queryString->nick); } public function testGetWithEqualSign() { $response = Request::get('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark=Hello' )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('Mark=Hello', $response->body->queryString->name); } public function testGetWithEqualSignAlt() { $response = Request::get('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark=Hello=John' )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('Mark=Hello=John', $response->body->queryString->name); } public function testGetWithComplexQuery() { $response = Request::get('http://mockbin.com/request?query=[{"type":"/music/album","name":null,"artist":{"id":"/en/bob_dylan"},"limit":3}]&cursor'); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('', $response->body->queryString->cursor); $this->assertEquals('[{"type":"/music/album","name":null,"artist":{"id":"/en/bob_dylan"},"limit":3}]', $response->body->queryString->query); } public function testGetArray() { $response = Request::get('http://mockbin.com/request', array(), array( 'name[0]' => 'Mark', 'name[1]' => 'John' )); $this->assertEquals(200, $response->code); $this->assertEquals('GET', $response->body->method); $this->assertEquals('Mark', $response->body->queryString->name[0]); $this->assertEquals('John', $response->body->queryString->name[1]); } // POST public function testPost() { $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('thefosk', $response->body->postData->params->nick); } public function testPostForm() { $body = Request\Body::Form(array( 'name' => 'Mark', 'nick' => 'thefosk' )); $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), $body); $this->assertEquals('POST', $response->body->method); $this->assertEquals('application/x-www-form-urlencoded', $response->body->headers->{'content-type'}); $this->assertEquals('application/x-www-form-urlencoded', $response->body->postData->mimeType); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('thefosk', $response->body->postData->params->nick); } public function testPostMultipart() { $body = Request\Body::Multipart(array( 'name' => 'Mark', 'nick' => 'thefosk' )); $response = Request::post('http://mockbin.com/request', (object) array( 'Accept' => 'application/json', ), $body); $this->assertEquals('POST', $response->body->method); $this->assertEquals('multipart/form-data', explode(';', $response->body->headers->{'content-type'})[0]); $this->assertEquals('multipart/form-data', $response->body->postData->mimeType); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('thefosk', $response->body->postData->params->nick); } public function testPostWithEqualSign() { $body = Request\Body::Form(array( 'name' => 'Mark=Hello' )); $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), $body); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Mark=Hello', $response->body->postData->params->name); } public function testPostArray() { $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name[0]' => 'Mark', 'name[1]' => 'John' )); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->{'name[0]'}); $this->assertEquals('John', $response->body->postData->params->{'name[1]'}); } public function testPostWithDots() { $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'user.name' => 'Mark', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->{'user.name'}); $this->assertEquals('thefosk', $response->body->postData->params->nick); } public function testRawPost() { $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json', 'Content-Type' => 'application/json' ), json_encode(array( 'author' => 'Sam Sullivan' ))); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Sam Sullivan', json_decode($response->body->postData->text)->author); } public function testPostMultidimensionalArray() { $body = Request\Body::Form(array( 'key' => 'value', 'items' => array( 'item1', 'item2' ) )); $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), $body); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('value', $response->body->postData->params->key); $this->assertEquals('item1', $response->body->postData->params->{'items[0]'}); $this->assertEquals('item2', $response->body->postData->params->{'items[1]'}); } // PUT public function testPut() { $response = Request::put('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('PUT', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('thefosk', $response->body->postData->params->nick); } // PATCH public function testPatch() { $response = Request::patch('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('PATCH', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('thefosk', $response->body->postData->params->nick); } // DELETE public function testDelete() { $response = Request::delete('http://mockbin.com/request', array( 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded' ), array( 'name' => 'Mark', 'nick' => 'thefosk' )); $this->assertEquals(200, $response->code); $this->assertEquals('DELETE', $response->body->method); } // Upload public function testUpload() { $fixture = __DIR__ . '/../fixtures/upload.txt'; $headers = array('Accept' => 'application/json'); $files = array('file' => $fixture); $data = array('name' => 'ahmad'); $body = Request\Body::Multipart($data, $files); $response = Request::post('http://mockbin.com/request', $headers, $body); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('ahmad', $response->body->postData->params->name); $this->assertEquals('This is a test', $response->body->postData->params->file); } public function testUploadWithoutHelper() { $fixture = __DIR__ . '/../fixtures/upload.txt'; $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark', 'file' => Request\Body::File($fixture) )); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('This is a test', $response->body->postData->params->file); } public function testUploadIfFilePartOfData() { $fixture = __DIR__ . '/../fixtures/upload.txt'; $response = Request::post('http://mockbin.com/request', array( 'Accept' => 'application/json' ), array( 'name' => 'Mark', 'files[owl.gif]' => Request\Body::File($fixture) )); $this->assertEquals(200, $response->code); $this->assertEquals('POST', $response->body->method); $this->assertEquals('Mark', $response->body->postData->params->name); $this->assertEquals('This is a test', $response->body->postData->params->{'files[owl.gif]'}); } } PKZ2z%unirest-php/tests/fixtures/upload.txtnu[This is a testPKZ$ unirest-php/tests/bootstrap.phpnu[ PKZt t $unirest-php/src/Unirest/Response.phpnu[code = $code; $this->headers = $this->parseHeaders($headers); $this->raw_body = $raw_body; $this->body = $raw_body; // make sure raw_body is the first argument array_unshift($json_args, $raw_body); if (function_exists('json_decode')) { $json = call_user_func_array('json_decode', $json_args); if (json_last_error() === JSON_ERROR_NONE) { $this->body = $json; } } } /** * if PECL_HTTP is not available use a fall back function * * thanks to ricardovermeltfoort@gmail.com * http://php.net/manual/en/function.http-parse-headers.php#112986 * @param string $raw_headers raw headers * @return array */ private function parseHeaders($raw_headers) { if (function_exists('http_parse_headers')) { return http_parse_headers($raw_headers); } else { $key = ''; $headers = array(); foreach (explode("\n", $raw_headers) as $i => $h) { $h = explode(':', $h, 2); if (isset($h[1])) { if (!isset($headers[$h[0]])) { $headers[$h[0]] = trim($h[1]); } elseif (is_array($headers[$h[0]])) { $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); } else { $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); } $key = $h[0]; } else { if (substr($h[0], 0, 1) == "\t") { $headers[$key] .= "\r\n\t".trim($h[0]); } elseif (!$key) { $headers[0] = trim($h[0]); } } } return $headers; } } } PKZ?AA%unirest-php/src/Unirest/Exception.phpnu[ $file) { $data[$name] = call_user_func(array(__CLASS__, 'File'), $file); } } return $data; } } PKZFmKK#unirest-php/src/Unirest/Request.phpnu[ '', 'pass' => '', 'method' => CURLAUTH_BASIC ); private static $proxy = array( 'port' => false, 'tunnel' => false, 'address' => false, 'type' => CURLPROXY_HTTP, 'auth' => array ( 'user' => '', 'pass' => '', 'method' => CURLAUTH_BASIC ) ); /** * Set JSON decode mode * * @param bool $assoc When TRUE, returned objects will be converted into associative arrays. * @param integer $depth User specified recursion depth. * @param integer $options Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats) * @return array */ public static function jsonOpts($assoc = false, $depth = 512, $options = 0) { return self::$jsonOpts = array($assoc, $depth, $options); } /** * Verify SSL peer * * @param bool $enabled enable SSL verification, by default is true * @return bool */ public static function verifyPeer($enabled) { return self::$verifyPeer = $enabled; } /** * Verify SSL host * * @param bool $enabled enable SSL host verification, by default is true * @return bool */ public static function verifyHost($enabled) { return self::$verifyHost = $enabled; } /** * Set a timeout * * @param integer $seconds timeout value in seconds * @return integer */ public static function timeout($seconds) { return self::$socketTimeout = $seconds; } /** * Set default headers to send on every request * * @param array $headers headers array * @return array */ public static function defaultHeaders($headers) { return self::$defaultHeaders = array_merge(self::$defaultHeaders, $headers); } /** * Set a new default header to send on every request * * @param string $name header name * @param string $value header value * @return string */ public static function defaultHeader($name, $value) { return self::$defaultHeaders[$name] = $value; } /** * Clear all the default headers */ public static function clearDefaultHeaders() { return self::$defaultHeaders = array(); } /** * Set curl options to send on every request * * @param array $options options array * @return array */ public static function curlOpts($options) { return self::mergeCurlOptions(self::$curlOpts, $options); } /** * Set a new default header to send on every request * * @param string $name header name * @param string $value header value * @return string */ public static function curlOpt($name, $value) { return self::$curlOpts[$name] = $value; } /** * Clear all the default headers */ public static function clearCurlOpts() { return self::$curlOpts = array(); } /** * Set a Mashape key to send on every request as a header * Obtain your Mashape key by browsing one of your Mashape applications on https://www.mashape.com * * Note: Mashape provides 2 keys for each application: a 'Testing' and a 'Production' one. * Be aware of which key you are using and do not share your Production key. * * @param string $key Mashape key * @return string */ public static function setMashapeKey($key) { return self::defaultHeader('X-Mashape-Key', $key); } /** * Set a cookie string for enabling cookie handling * * @param string $cookie */ public static function cookie($cookie) { self::$cookie = $cookie; } /** * Set a cookie file path for enabling cookie handling * * $cookieFile must be a correct path with write permission * * @param string $cookieFile - path to file for saving cookie */ public static function cookieFile($cookieFile) { self::$cookieFile = $cookieFile; } /** * Set authentication method to use * * @param string $username authentication username * @param string $password authentication password * @param integer $method authentication method */ public static function auth($username = '', $password = '', $method = CURLAUTH_BASIC) { self::$auth['user'] = $username; self::$auth['pass'] = $password; self::$auth['method'] = $method; } /** * Set proxy to use * * @param string $address proxy address * @param integer $port proxy port * @param integer $type (Available options for this are CURLPROXY_HTTP, CURLPROXY_HTTP_1_0 CURLPROXY_SOCKS4, CURLPROXY_SOCKS5, CURLPROXY_SOCKS4A and CURLPROXY_SOCKS5_HOSTNAME) * @param bool $tunnel enable/disable tunneling */ public static function proxy($address, $port = 1080, $type = CURLPROXY_HTTP, $tunnel = false) { self::$proxy['type'] = $type; self::$proxy['port'] = $port; self::$proxy['tunnel'] = $tunnel; self::$proxy['address'] = $address; } /** * Set proxy authentication method to use * * @param string $username authentication username * @param string $password authentication password * @param integer $method authentication method */ public static function proxyAuth($username = '', $password = '', $method = CURLAUTH_BASIC) { self::$proxy['auth']['user'] = $username; self::$proxy['auth']['pass'] = $password; self::$proxy['auth']['method'] = $method; } /** * Send a GET request to a URL * * @param string $url URL to send the GET request to * @param array $headers additional headers to send * @param mixed $parameters parameters to send in the querystring * @param string $username Authentication username (deprecated) * @param string $password Authentication password (deprecated) * @return Response */ public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null) { return self::send(Method::GET, $url, $parameters, $headers, $username, $password); } /** * Send a HEAD request to a URL * @param string $url URL to send the HEAD request to * @param array $headers additional headers to send * @param mixed $parameters parameters to send in the querystring * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response */ public static function head($url, $headers = array(), $parameters = null, $username = null, $password = null) { return self::send(Method::HEAD, $url, $parameters, $headers, $username, $password); } /** * Send a OPTIONS request to a URL * @param string $url URL to send the OPTIONS request to * @param array $headers additional headers to send * @param mixed $parameters parameters to send in the querystring * @param string $username Basic Authentication username * @param string $password Basic Authentication password * @return Response */ public static function options($url, $headers = array(), $parameters = null, $username = null, $password = null) { return self::send(Method::OPTIONS, $url, $parameters, $headers, $username, $password); } /** * Send a CONNECT request to a URL * @param string $url URL to send the CONNECT request to * @param array $headers additional headers to send * @param mixed $parameters parameters to send in the querystring * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response */ public static function connect($url, $headers = array(), $parameters = null, $username = null, $password = null) { return self::send(Method::CONNECT, $url, $parameters, $headers, $username, $password); } /** * Send POST request to a URL * @param string $url URL to send the POST request to * @param array $headers additional headers to send * @param mixed $body POST body data * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response response */ public static function post($url, $headers = array(), $body = null, $username = null, $password = null) { return self::send(Method::POST, $url, $body, $headers, $username, $password); } /** * Send DELETE request to a URL * @param string $url URL to send the DELETE request to * @param array $headers additional headers to send * @param mixed $body DELETE body data * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response */ public static function delete($url, $headers = array(), $body = null, $username = null, $password = null) { return self::send(Method::DELETE, $url, $body, $headers, $username, $password); } /** * Send PUT request to a URL * @param string $url URL to send the PUT request to * @param array $headers additional headers to send * @param mixed $body PUT body data * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response */ public static function put($url, $headers = array(), $body = null, $username = null, $password = null) { return self::send(Method::PUT, $url, $body, $headers, $username, $password); } /** * Send PATCH request to a URL * @param string $url URL to send the PATCH request to * @param array $headers additional headers to send * @param mixed $body PATCH body data * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response */ public static function patch($url, $headers = array(), $body = null, $username = null, $password = null) { return self::send(Method::PATCH, $url, $body, $headers, $username, $password); } /** * Send TRACE request to a URL * @param string $url URL to send the TRACE request to * @param array $headers additional headers to send * @param mixed $body TRACE body data * @param string $username Basic Authentication username (deprecated) * @param string $password Basic Authentication password (deprecated) * @return Response */ public static function trace($url, $headers = array(), $body = null, $username = null, $password = null) { return self::send(Method::TRACE, $url, $body, $headers, $username, $password); } /** * This function is useful for serializing multidimensional arrays, and avoid getting * the 'Array to string conversion' notice * @param array|object $data array to flatten. * @param bool|string $parent parent key or false if no parent * @return array */ public static function buildHTTPCurlQuery($data, $parent = false) { $result = array(); if (is_object($data)) { $data = get_object_vars($data); } foreach ($data as $key => $value) { if ($parent) { $new_key = sprintf('%s[%s]', $parent, $key); } else { $new_key = $key; } if (!$value instanceof \CURLFile and (is_array($value) or is_object($value))) { $result = array_merge($result, self::buildHTTPCurlQuery($value, $new_key)); } else { $result[$new_key] = $value; } } return $result; } /** * Send a cURL request * @param \Unirest\Method|string $method HTTP method to use * @param string $url URL to send the request to * @param mixed $body request body * @param array $headers additional headers to send * @param string $username Authentication username (deprecated) * @param string $password Authentication password (deprecated) * @throws \Unirest\Exception if a cURL error occurs * @return Response */ public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null) { self::$handle = curl_init(); if ($method !== Method::GET) { if ($method === Method::POST) { curl_setopt(self::$handle, CURLOPT_POST, true); } else { curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method); } curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body); } elseif (is_array($body)) { if (strpos($url, '?') !== false) { $url .= '&'; } else { $url .= '?'; } $url .= urldecode(http_build_query(self::buildHTTPCurlQuery($body))); } $curl_base_options = [ CURLOPT_URL => self::encodeUrl($url), CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 10, CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers), CURLOPT_HEADER => true, CURLOPT_SSL_VERIFYPEER => self::$verifyPeer, //CURLOPT_SSL_VERIFYHOST accepts only 0 (false) or 2 (true). Future versions of libcurl will treat values 1 and 2 as equals CURLOPT_SSL_VERIFYHOST => self::$verifyHost === false ? 0 : 2, // If an empty string, '', is set, a header containing all supported encoding types is sent CURLOPT_ENCODING => '' ]; curl_setopt_array(self::$handle, self::mergeCurlOptions($curl_base_options, self::$curlOpts)); if (self::$socketTimeout !== null) { curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout); } if (self::$cookie) { curl_setopt(self::$handle, CURLOPT_COOKIE, self::$cookie); } if (self::$cookieFile) { curl_setopt(self::$handle, CURLOPT_COOKIEFILE, self::$cookieFile); curl_setopt(self::$handle, CURLOPT_COOKIEJAR, self::$cookieFile); } // supporting deprecated http auth method if (!empty($username)) { curl_setopt_array(self::$handle, array( CURLOPT_HTTPAUTH => CURLAUTH_BASIC, CURLOPT_USERPWD => $username . ':' . $password )); } if (!empty(self::$auth['user'])) { curl_setopt_array(self::$handle, array( CURLOPT_HTTPAUTH => self::$auth['method'], CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass'] )); } if (self::$proxy['address'] !== false) { curl_setopt_array(self::$handle, array( CURLOPT_PROXYTYPE => self::$proxy['type'], CURLOPT_PROXY => self::$proxy['address'], CURLOPT_PROXYPORT => self::$proxy['port'], CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'], CURLOPT_PROXYAUTH => self::$proxy['auth']['method'], CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass'] )); } $response = curl_exec(self::$handle); $error = curl_error(self::$handle); $info = self::getInfo(); if ($error) { throw new Exception($error); } // Split the full response in its headers and body $header_size = $info['header_size']; $header = substr($response, 0, $header_size); $body = substr($response, $header_size); $httpCode = $info['http_code']; return new Response($httpCode, $body, $header, self::$jsonOpts); } public static function getInfo($opt = false) { if ($opt) { $info = curl_getinfo(self::$handle, $opt); } else { $info = curl_getinfo(self::$handle); } return $info; } public static function getCurlHandle() { return self::$handle; } public static function getFormattedHeaders($headers) { $formattedHeaders = array(); $combinedHeaders = array_change_key_case(array_merge(self::$defaultHeaders, (array) $headers)); foreach ($combinedHeaders as $key => $val) { $formattedHeaders[] = self::getHeaderString($key, $val); } if (!array_key_exists('user-agent', $combinedHeaders)) { $formattedHeaders[] = 'user-agent: unirest-php/2.0'; } if (!array_key_exists('expect', $combinedHeaders)) { $formattedHeaders[] = 'expect:'; } return $formattedHeaders; } private static function getArrayFromQuerystring($query) { $query = preg_replace_callback('/(?:^|(?<=&))[^=[]+/', function ($match) { return bin2hex(urldecode($match[0])); }, $query); parse_str($query, $values); return array_combine(array_map('hex2bin', array_keys($values)), $values); } /** * Ensure that a URL is encoded and safe to use with cURL * @param string $url URL to encode * @return string */ private static function encodeUrl($url) { $url_parsed = parse_url($url); $scheme = $url_parsed['scheme'] . '://'; $host = $url_parsed['host']; $port = (isset($url_parsed['port']) ? $url_parsed['port'] : null); $path = (isset($url_parsed['path']) ? $url_parsed['path'] : null); $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); if ($query !== null) { $query = '?' . http_build_query(self::getArrayFromQuerystring($query)); } if ($port && $port[0] !== ':') { $port = ':' . $port; } $result = $scheme . $host . $port . $path . $query; return $result; } private static function getHeaderString($key, $val) { $key = trim(strtolower($key)); return $key . ': ' . $val; } /** * @param array $existing_options * @param array $new_options * @return array */ private static function mergeCurlOptions(&$existing_options, $new_options) { $existing_options = $new_options + $existing_options; return $existing_options; } } PKZ>7s11"unirest-php/src/Unirest/Method.phpnu[ 'application/json'); $query = array('foo' => 'hello', 'bar' => 'world'); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $query); $response->code; // HTTP Status code $response->headers; // Headers $response->body; // Parsed body $response->raw_body; // Unparsed body ``` ### JSON Requests *(`application/json`)* A JSON Request can be constructed using the `Unirest\Request\Body::Json` helper: ```php $headers = array('Accept' => 'application/json'); $data = array('name' => 'ahmad', 'company' => 'mashape'); $body = Unirest\Request\Body::json($data); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $body); ``` **Notes:** - `Content-Type` headers will be automatically set to `application/json` - the data variable will be processed through [`json_encode`](http://php.net/manual/en/function.json-encode.php) with default values for arguments. - an error will be thrown if the [JSON Extension](http://php.net/manual/en/book.json.php) is not available. ### Form Requests *(`application/x-www-form-urlencoded`)* A typical Form Request can be constructed using the `Unirest\Request\Body::Form` helper: ```php $headers = array('Accept' => 'application/json'); $data = array('name' => 'ahmad', 'company' => 'mashape'); $body = Unirest\Request\Body::form($data); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $body); ``` **Notes:** - `Content-Type` headers will be automatically set to `application/x-www-form-urlencoded` - the final data array will be processed through [`http_build_query`](http://php.net/manual/en/function.http-build-query.php) with default values for arguments. ### Multipart Requests *(`multipart/form-data`)* A Multipart Request can be constructed using the `Unirest\Request\Body::Multipart` helper: ```php $headers = array('Accept' => 'application/json'); $data = array('name' => 'ahmad', 'company' => 'mashape'); $body = Unirest\Request\Body::multipart($data); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $body); ``` **Notes:** - `Content-Type` headers will be automatically set to `multipart/form-data`. - an auto-generated `--boundary` will be set. ### Multipart File Upload simply add an array of files as the second argument to to the `Multipart` helper: ```php $headers = array('Accept' => 'application/json'); $data = array('name' => 'ahmad', 'company' => 'mashape'); $files = array('bio' => '/path/to/bio.txt', 'avatar' => '/path/to/avatar.jpg'); $body = Unirest\Request\Body::multipart($data, $files); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $body); ``` If you wish to further customize the properties of files uploaded you can do so with the `Unirest\Request\Body::File` helper: ```php $headers = array('Accept' => 'application/json'); $body = array( 'name' => 'ahmad', 'company' => 'mashape' 'bio' => Unirest\Request\Body::file('/path/to/bio.txt', 'text/plain'), 'avatar' => Unirest\Request\Body::file('/path/to/my_avatar.jpg', 'text/plain', 'avatar.jpg') ); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $body); ``` **Note**: we did not use the `Unirest\Request\Body::multipart` helper in this example, it is not needed when manually adding files. ### Custom Body Sending a custom body such rather than using the `Unirest\Request\Body` helpers is also possible, for example, using a [`serialize`](http://php.net/manual/en/function.serialize.php) body string with a custom `Content-Type`: ```php $headers = array('Accept' => 'application/json', 'Content-Type' => 'application/x-php-serialized'); $body = serialize((array('foo' => 'hello', 'bar' => 'world')); $response = Unirest\Request::post('http://mockbin.com/request', $headers, $body); ``` ### Authentication First, if you are using [Mashape][mashape-url]: ```php // Mashape auth Unirest\Request::setMashapeKey(''); ``` Otherwise, passing a username, password *(optional)*, defaults to Basic Authentication: ```php // basic auth Unirest\Request::auth('username', 'password'); ``` The third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication. If more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.* **Supported Methods** | Method | Description | | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `CURLAUTH_BASIC` | HTTP Basic authentication. This is the default choice | | `CURLAUTH_DIGEST` | HTTP Digest authentication. as defined in [RFC 2617](http://www.ietf.org/rfc/rfc2617.txt) | | `CURLAUTH_DIGEST_IE` | HTTP Digest authentication with an IE flavor. *The IE flavor is simply that libcurl will use a special "quirk" that IE is known to have used before version 7 and that some servers require the client to use.* | | `CURLAUTH_NEGOTIATE` | HTTP Negotiate (SPNEGO) authentication. as defined in [RFC 4559](http://www.ietf.org/rfc/rfc4559.txt) | | `CURLAUTH_NTLM` | HTTP NTLM authentication. A proprietary protocol invented and used by Microsoft. | | `CURLAUTH_NTLM_WB` | NTLM delegating to winbind helper. Authentication is performed by a separate binary application. *see [libcurl docs](http://curl.haxx.se/libcurl/c/CURLOPT_HTTPAUTH.html) for more info* | | `CURLAUTH_ANY` | This is a convenience macro that sets all bits and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure. | | `CURLAUTH_ANYSAFE` | This is a convenience macro that sets all bits except Basic and thus makes libcurl pick any it finds suitable. libcurl will automatically select the one it finds most secure. | | `CURLAUTH_ONLY` | This is a meta symbol. OR this value together with a single specific auth value to force libcurl to probe for un-restricted auth and if not, only that single auth algorithm is acceptable. | ```php // custom auth method Unirest\Request::proxyAuth('username', 'password', CURLAUTH_DIGEST); ``` Previous versions of **Unirest** support *Basic Authentication* by providing the `username` and `password` arguments: ```php $response = Unirest\Request::get('http://mockbin.com/request', null, null, 'username', 'password'); ``` **This has been deprecated, and will be completely removed in `v.3.0.0` please use the `Unirest\Request::auth()` method instead** ### Cookies Set a cookie string to specify the contents of a cookie header. Multiple cookies are separated with a semicolon followed by a space (e.g., "fruit=apple; colour=red") ```php Unirest\Request::cookie($cookie) ``` Set a cookie file path for enabling cookie reading and storing cookies across multiple sequence of requests. ```php Unirest\Request::cookieFile($cookieFile) ``` `$cookieFile` must be a correct path with write permission. ### Request Object ```php Unirest\Request::get($url, $headers = array(), $parameters = null) Unirest\Request::post($url, $headers = array(), $body = null) Unirest\Request::put($url, $headers = array(), $body = null) Unirest\Request::patch($url, $headers = array(), $body = null) Unirest\Request::delete($url, $headers = array(), $body = null) ``` - `url` - Endpoint, address, or uri to be acted upon and requested information from. - `headers` - Request Headers as associative array or object - `body` - Request Body as associative array or object You can send a request with any [standard](http://www.iana.org/assignments/http-methods/http-methods.xhtml) or custom HTTP Method: ```php Unirest\Request::send(Unirest\Method::LINK, $url, $headers = array(), $body); Unirest\Request::send('CHECKOUT', $url, $headers = array(), $body); ``` ### Response Object Upon recieving a response Unirest returns the result in the form of an Object, this object should always have the same keys for each language regarding to the response details. - `code` - HTTP Response Status Code (Example `200`) - `headers` - HTTP Response Headers - `body` - Parsed response body where applicable, for example JSON responses are parsed to Objects / Associative Arrays. - `raw_body` - Un-parsed response body ### Advanced Configuration You can set some advanced configuration to tune Unirest-PHP: #### Custom JSON Decode Flags Unirest uses PHP's [JSON Extension](http://php.net/manual/en/book.json.php) for automatically decoding JSON responses. sometime you may want to return associative arrays, limit the depth of recursion, or use any of the [customization flags](http://php.net/manual/en/json.constants.php). To do so, simply set the desired options using the `jsonOpts` request method: ```php Unirest\Request::jsonOpts(true, 512, JSON_NUMERIC_CHECK & JSON_FORCE_OBJECT & JSON_UNESCAPED_SLASHES); ``` #### Timeout You can set a custom timeout value (in **seconds**): ```php Unirest\Request::timeout(5); // 5s timeout ``` #### Proxy Set the proxy to use for the upcoming request. you can also set the proxy type to be one of `CURLPROXY_HTTP`, `CURLPROXY_HTTP_1_0`, `CURLPROXY_SOCKS4`, `CURLPROXY_SOCKS5`, `CURLPROXY_SOCKS4A`, and `CURLPROXY_SOCKS5_HOSTNAME`. *check the [cURL docs](http://curl.haxx.se/libcurl/c/CURLOPT_PROXYTYPE.html) for more info*. ```php // quick setup with default port: 1080 Unirest\Request::proxy('10.10.10.1'); // custom port and proxy type Unirest\Request::proxy('10.10.10.1', 8080, CURLPROXY_HTTP); // enable tunneling Unirest\Request::proxy('10.10.10.1', 8080, CURLPROXY_HTTP, true); ``` ##### Proxy Authenticaton Passing a username, password *(optional)*, defaults to Basic Authentication: ```php // basic auth Unirest\Request::proxyAuth('username', 'password'); ``` The third parameter, which is a bitmask, will Unirest which HTTP authentication method(s) you want it to use for your proxy authentication. If more than one bit is set, Unirest *(at PHP's libcurl level)* will first query the site to see what authentication methods it supports and then pick the best one you allow it to use. *For some methods, this will induce an extra network round-trip.* See [Authentication](#authentication) for more details on methods supported. ```php // basic auth Unirest\Request::proxyAuth('username', 'password', CURLAUTH_DIGEST); ``` #### Default Request Headers You can set default headers that will be sent on every request: ```php Unirest\Request::defaultHeader('Header1', 'Value1'); Unirest\Request::defaultHeader('Header2', 'Value2'); ``` You can set default headers in bulk by passing an array: ```php Unirest\Request::defaultHeaders(array( 'Header1' => 'Value1', 'Header2' => 'Value2' )); ``` You can clear the default headers anytime with: ```php Unirest\Request::clearDefaultHeaders(); ``` #### Default cURL Options You can set default [cURL options](http://php.net/manual/en/function.curl-setopt.php) that will be sent on every request: ```php Unirest\Request::curlOpt(CURLOPT_COOKIE, 'foo=bar'); ``` You can set options bulk by passing an array: ```php Unirest\Request::curlOpts(array( CURLOPT_COOKIE => 'foo=bar' )); ``` You can clear the default options anytime with: ```php Unirest\Request::clearCurlOpts(); ``` #### SSL validation You can explicitly enable or disable SSL certificate validation when consuming an SSL protected endpoint: ```php Unirest\Request::verifyPeer(false); // Disables SSL cert validation ``` By default is `true`. #### Utility Methods ```php // alias for `curl_getinfo` Unirest\Request::getInfo() // returns internal cURL handle Unirest\Request::getCurlHandle() ``` ---- Made with ♥ from the [Mashape][mashape-url] team [unirest-logo]: http://cl.ly/image/2P373Y090s2O/Image%202015-10-12%20at%209.48.06%20PM.png [mashape-url]: https://www.mashape.com/ [license-url]: https://github.com/Mashape/unirest-php/blob/master/LICENSE [gitter-url]: https://gitter.im/Mashape/unirest-php [gitter-image]: https://img.shields.io/badge/Gitter-Join%20Chat-blue.svg?style=flat [travis-url]: https://travis-ci.org/Mashape/unirest-php [travis-image]: https://img.shields.io/travis/Mashape/unirest-php.svg?style=flat [packagist-url]: https://packagist.org/packages/Mashape/unirest-php [packagist-license]: https://img.shields.io/packagist/l/Mashape/unirest-php.svg?style=flat [packagist-version]: https://img.shields.io/packagist/v/Mashape/unirest-php.svg?style=flat [packagist-downloads]: https://img.shields.io/packagist/dm/Mashape/unirest-php.svg?style=flat [codeclimate-url]: https://codeclimate.com/github/Mashape/unirest-php [codeclimate-quality]: https://img.shields.io/codeclimate/github/Mashape/unirest-php.svg?style=flat [codeclimate-coverage]: https://img.shields.io/codeclimate/coverage/github/Mashape/unirest-php.svg?style=flat [versioneye-url]: https://www.versioneye.com/user/projects/54b82450050646ca5c0001f3 [versioneye-image]: https://img.shields.io/versioneye/d/php/mashape:unirest-php.svg?style=flat PKZ5c|unirest-php/.travis.ymlnu[sudo: false language: php php: - '5.4' - '5.5' - '5.6' - '7.0' - hhvm before_script: - composer selfupdate - composer install after_script: - vendor/bin/test-reporter --stdout > codeclimate.json - "curl -X POST -d @codeclimate.json -H 'Content-Type: application/json' -H 'User-Agent: Code Climate (PHP Test Reporter v0.1.1)' https://codeclimate.com/test_reports" notifications: webhooks: urls: - https://webhooks.gitter.im/e/802f417bb6e3e1e8b20d on_success: always on_failure: always on_start: false matrix: fast_finish: true allow_failures: - php: hhvm notifications: webhooks: urls: - https://webhooks.gitter.im/e/d4319553d0aecfd5b9ac on_success: always on_failure: always on_start: false PKZ^"Munirest-php/.editorconfignu[# http://editorconfig.org root = true [*] indent_style = space indent_size = 4 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.{json,xml,yml}] indent_size = 2 [*.{md,yml}] trim_trailing_whitespace = false PKZd[KKunirest-php/LICENSEnu[The MIT License Copyright (c) 2013-2015 Mashape (https://www.mashape.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PKZQoBBunirest-php/.gitignorenu[.DS_Store .idea build composer.lock composer.phar coverage vendor PKZ|unirest-php/phpunit.xmlnu[ ./tests PKZAunirest-php/composer.jsonnu[PKZ7ʑ&unirest-php/tests/Unirest/BodyTest.phpnu[PKZ_{|* unirest-php/tests/Unirest/ResponseTest.phpnu[PKZBB)unirest-php/tests/Unirest/RequestTest.phpnu[PKZ2z%Runirest-php/tests/fixtures/upload.txtnu[PKZ$ -Sunirest-php/tests/bootstrap.phpnu[PKZt t $Sunirest-php/src/Unirest/Response.phpnu[PKZ?AA%M]unirest-php/src/Unirest/Exception.phpnu[PKZG"(]unirest-php/src/Unirest/Request/Body.phpnu[PKZFmKK#dunirest-php/src/Unirest/Request.phpnu[PKZ>7s11"unirest-php/src/Unirest/Method.phpnu[PKZaM++vunirest-php/src/Unirest.phpnu[PKZ-JPў@@unirest-php/README.mdnu[PKZ5c|unirest-php/.travis.ymlnu[PKZ^"Munirest-php/.editorconfignu[PKZd[KKcunirest-php/LICENSEnu[PKZQoBBunirest-php/.gitignorenu[PKZ|yunirest-php/phpunit.xmlnu[PKB