9 Commits

Author SHA1 Message Date
Kieran O'Reilly
842c0f242d Fix composer package (#100)
* Update composer.json

* Remove trailing comma
2016-08-11 13:49:21 -04:00
İrfan Evrens
5d304c6f6c improved syntax and some codes. (#108)
* improved syntax and some codes.

* back to original for Body.php
2016-05-31 12:59:14 -04:00
Ahmad Nassri
53656bf519 Merge pull request #105 from voodoodrul/patch-2
Don't use CURLOPT_CUSTOMREQUEST for POSTs
2016-04-29 19:54:44 -04:00
Ahmad Nassri
889ca14f5f Merge pull request #106 from Lozzano/master
Fixing current version in README.
2016-04-25 10:03:29 -04:00
André Valentin
45274fd536 Fixing current version in README.
The current version wasn't showed as the one people should add in their composer files. Updated from 2.* to 3.*.
2016-04-25 15:02:42 +02:00
Jesse Skrivseth
a3446f6d99 Update Request.php
Remove curl_setopt(self::$handle, CURLOPT_POSTREDIR, 3);  in favor of the caller being well-behaved and setting this option as needed
2016-04-19 23:05:11 -06:00
Jesse Skrivseth
e9ea4dbee2 Don't use CURLOPT_CUSTOMREQUEST for POSTs
I think the current usage of CURLOPT_CUSTOMREQUEST is wrong. As far as I can tell CURLOPT_CUSTOMREQUEST = Method::POST is not the same thing as CURLOPT_POST = true. 

I struggled for hours with POSTed data not being accepted by IIS. First IIS threw an HTTP 411 due to curl not sending a Content-Length header. That is solved by replacing CURLOPT_CUSTOMREQUEST with CURLOPT_POST in the case of actual POSTs. Also, IIS in my case returns an HTTP 301 redirect, which I need Unirest to follow, so I set the CURLOPT_POSTREDIR = 3 in order to let the POST follow on redirect via 301 or 302. 

Notice this: https://curl.haxx.se/libcurl/c/CURLOPT_CUSTOMREQUEST.html - "Many people have wrongly used this option to replace the entire request with their own, including multiple headers and POST contents. While that might work in many cases, it will cause libcurl to send invalid requests and it could possibly confuse the remote server badly. Use CURLOPT_POST and CURLOPT_POSTFIELDS to set POST data. Use CURLOPT_HTTPHEADER to replace or extend the set of headers sent by libcurl. Use CURLOPT_HTTP_VERSION to change HTTP version."

In any case, I know that the CURLOPT_POSTREDIR option probably shouldn't go here and should be client configurable, but I can't find a good place for it. It would be nice to have one-shot $curlOpts passed in with the send method, or curlOpts() that otherwise live for only one curl_exec().
2016-04-19 22:49:27 -06:00
Colin Hutchinson
d71cd15d4a Merge pull request #102 from xcopy/master
Typofix
2016-04-14 08:33:01 -04:00
Kairat Jenishev
2d0cd4c8d9 Typofix 2016-04-14 17:01:32 +06:00
3 changed files with 22 additions and 21 deletions

View File

@@ -37,7 +37,7 @@ To install unirest-php with Composer, just add the following to your `composer.j
```json ```json
{ {
"require-dev": { "require-dev": {
"mashape/unirest-php": "2.*" "mashape/unirest-php": "3.*"
} }
} }
``` ```

View File

@@ -19,7 +19,7 @@
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {
"Unirest": "src" "Unirest\\": "src/"
} }
}, },
"support": { "support": {

View File

@@ -2,10 +2,6 @@
namespace Unirest; namespace Unirest;
use Unirest\Method as Method;
use Unirest\Response as Response;
use Unirest\Exception as Exception;
class Request class Request
{ {
private static $cookie = null; private static $cookie = null;
@@ -86,6 +82,7 @@ class Request
* Set default headers to send on every request * Set default headers to send on every request
* *
* @param array $headers headers array * @param array $headers headers array
* @return array
*/ */
public static function defaultHeaders($headers) public static function defaultHeaders($headers)
{ {
@@ -159,7 +156,7 @@ class Request
} }
/** /**
* Set a coockie string for enabling coockie handling * Set a cookie string for enabling cookie handling
* *
* @param string $cookie * @param string $cookie
*/ */
@@ -169,7 +166,7 @@ class Request
} }
/** /**
* Set a coockie file path for enabling coockie handling * Set a cookie file path for enabling cookie handling
* *
* $cookieFile must be a correct path with write permission * $cookieFile must be a correct path with write permission
* *
@@ -232,7 +229,7 @@ class Request
* @param mixed $parameters parameters to send in the querystring * @param mixed $parameters parameters to send in the querystring
* @param string $username Authentication username (deprecated) * @param string $username Authentication username (deprecated)
* @param string $password Authentication password (deprecated) * @param string $password Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null) public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null)
{ {
@@ -246,7 +243,7 @@ class Request
* @param mixed $parameters parameters to send in the querystring * @param mixed $parameters parameters to send in the querystring
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function head($url, $headers = array(), $parameters = null, $username = null, $password = null) public static function head($url, $headers = array(), $parameters = null, $username = null, $password = null)
{ {
@@ -260,7 +257,7 @@ class Request
* @param mixed $parameters parameters to send in the querystring * @param mixed $parameters parameters to send in the querystring
* @param string $username Basic Authentication username * @param string $username Basic Authentication username
* @param string $password Basic Authentication password * @param string $password Basic Authentication password
* @return Unirest\Response * @return Response
*/ */
public static function options($url, $headers = array(), $parameters = null, $username = null, $password = null) public static function options($url, $headers = array(), $parameters = null, $username = null, $password = null)
{ {
@@ -274,7 +271,7 @@ class Request
* @param mixed $parameters parameters to send in the querystring * @param mixed $parameters parameters to send in the querystring
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function connect($url, $headers = array(), $parameters = null, $username = null, $password = null) public static function connect($url, $headers = array(), $parameters = null, $username = null, $password = null)
{ {
@@ -288,7 +285,7 @@ class Request
* @param mixed $body POST body data * @param mixed $body POST body data
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response response * @return Response response
*/ */
public static function post($url, $headers = array(), $body = null, $username = null, $password = null) public static function post($url, $headers = array(), $body = null, $username = null, $password = null)
{ {
@@ -302,7 +299,7 @@ class Request
* @param mixed $body DELETE body data * @param mixed $body DELETE body data
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function delete($url, $headers = array(), $body = null, $username = null, $password = null) public static function delete($url, $headers = array(), $body = null, $username = null, $password = null)
{ {
@@ -316,7 +313,7 @@ class Request
* @param mixed $body PUT body data * @param mixed $body PUT body data
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function put($url, $headers = array(), $body = null, $username = null, $password = null) public static function put($url, $headers = array(), $body = null, $username = null, $password = null)
{ {
@@ -330,7 +327,7 @@ class Request
* @param mixed $body PATCH body data * @param mixed $body PATCH body data
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function patch($url, $headers = array(), $body = null, $username = null, $password = null) public static function patch($url, $headers = array(), $body = null, $username = null, $password = null)
{ {
@@ -344,7 +341,7 @@ class Request
* @param mixed $body TRACE body data * @param mixed $body TRACE body data
* @param string $username Basic Authentication username (deprecated) * @param string $username Basic Authentication username (deprecated)
* @param string $password Basic Authentication password (deprecated) * @param string $password Basic Authentication password (deprecated)
* @return Unirest\Response * @return Response
*/ */
public static function trace($url, $headers = array(), $body = null, $username = null, $password = null) public static function trace($url, $headers = array(), $body = null, $username = null, $password = null)
{ {
@@ -385,21 +382,25 @@ class Request
/** /**
* Send a cURL request * Send a cURL request
* @param Unirest\Method|string $method HTTP method to use * @param \Unirest\Method|string $method HTTP method to use
* @param string $url URL to send the request to * @param string $url URL to send the request to
* @param mixed $body request body * @param mixed $body request body
* @param array $headers additional headers to send * @param array $headers additional headers to send
* @param string $username Authentication username (deprecated) * @param string $username Authentication username (deprecated)
* @param string $password Authentication password (deprecated) * @param string $password Authentication password (deprecated)
* @throws Unirest\Exception if a cURL error occurs * @throws \Unirest\Exception if a cURL error occurs
* @return Unirest\Response * @return Response
*/ */
public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null) public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null)
{ {
self::$handle = curl_init(); self::$handle = curl_init();
if ($method !== Method::GET) { 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_CUSTOMREQUEST, $method);
}
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body); curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
} elseif (is_array($body)) { } elseif (is_array($body)) {