mirror of
https://github.com/php-curl-class/php-curl-class.git
synced 2026-08-17 16:03:40 +00:00
Add methods like Curl::setGet() for each HTTP request method (#936)
* Adding methods like Curl::setGet() for each HTTP request method
Adding these methods to separate the request initialization from the
execution.
Methods added:
Curl::setDelete()
Curl::setGet()
Curl::setHead()
Curl::setOptions()
Curl::setPatch()
Curl::setPost()
Curl::setPut()
Curl::setSearch()
* Fix loading local test servers
Error message:
+++ declare -A pids
run_phpunit.sh: line 77: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
Error message:
+++ server_count=7
+++ pids=()
run_phpunit.sh: line 85: "7" - 1: syntax error: operand expected (error token is ""7" - 1")
* Clean up
This commit is contained in:
@@ -11,23 +11,19 @@ $multi_curl->complete(function ($instance) {
|
||||
});
|
||||
|
||||
$curl_1 = new Curl();
|
||||
$curl_1->setOpt(CURLOPT_POST, true);
|
||||
$curl_1->setOpt(CURLOPT_POSTFIELDS, [
|
||||
$curl_1->setPost('https://httpbin.org/post', [
|
||||
'to' => 'alice',
|
||||
'subject' => 'hi',
|
||||
'body' => 'hi Alice',
|
||||
]);
|
||||
$curl_1->setUrl('https://httpbin.org/post');
|
||||
$multi_curl->addCurl($curl_1);
|
||||
|
||||
$curl_2 = new Curl();
|
||||
$curl_2->setOpt(CURLOPT_POST, true);
|
||||
$curl_2->setOpt(CURLOPT_POSTFIELDS, [
|
||||
$curl_2->setPost('https://httpbin.org/post', [
|
||||
'to' => 'bob',
|
||||
'subject' => 'hi',
|
||||
'body' => 'hi Bob',
|
||||
]);
|
||||
$curl_2->setUrl('https://httpbin.org/post');
|
||||
$multi_curl->addCurl($curl_2);
|
||||
|
||||
$multi_curl->start();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use Curl\Curl;
|
||||
use Curl\MultiCurl;
|
||||
|
||||
$multi_curl = new MultiCurl();
|
||||
$multi_curl->complete(function ($instance) {
|
||||
echo 'call to "' . $instance->url . '" completed.' . "\n";
|
||||
});
|
||||
|
||||
$curl_1 = new Curl();
|
||||
$curl_1->setOpt(CURLOPT_POST, true);
|
||||
$curl_1->setOpt(CURLOPT_POSTFIELDS, [
|
||||
'to' => 'alice',
|
||||
'subject' => 'hi',
|
||||
'body' => 'hi Alice',
|
||||
]);
|
||||
$curl_1->setUrl('https://httpbin.org/post');
|
||||
$multi_curl->addCurl($curl_1);
|
||||
|
||||
$curl_2 = new Curl();
|
||||
$curl_2->setOpt(CURLOPT_POST, true);
|
||||
$curl_2->setOpt(CURLOPT_POSTFIELDS, [
|
||||
'to' => 'bob',
|
||||
'subject' => 'hi',
|
||||
'body' => 'hi Bob',
|
||||
]);
|
||||
$curl_2->setUrl('https://httpbin.org/post');
|
||||
$multi_curl->addCurl($curl_2);
|
||||
|
||||
$multi_curl->start();
|
||||
+48
-8
@@ -275,6 +275,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function delete($url, $query_parameters = [], $data = [])
|
||||
{
|
||||
$this->setDelete($url, $query_parameters, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setDelete($url, $query_parameters = [], $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $query_parameters;
|
||||
@@ -295,7 +301,6 @@ class Curl extends BaseCurl
|
||||
if (!empty($data)) {
|
||||
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
|
||||
}
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -605,6 +610,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function get($url, $data = [])
|
||||
{
|
||||
$this->setGet($url, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setGet($url, $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $url;
|
||||
@@ -613,7 +624,6 @@ class Curl extends BaseCurl
|
||||
$this->setUrl($url, $data);
|
||||
$this->setOptInternal(CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
$this->setOptInternal(CURLOPT_HTTPGET, true);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -642,6 +652,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function head($url, $data = [])
|
||||
{
|
||||
$this->setHead($url, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setHead($url, $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $url;
|
||||
@@ -650,7 +666,6 @@ class Curl extends BaseCurl
|
||||
$this->setUrl($url, $data);
|
||||
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'HEAD');
|
||||
$this->setOpt(CURLOPT_NOBODY, true);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -661,6 +676,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function options($url, $data = [])
|
||||
{
|
||||
$this->setOptions($url, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setOptions($url, $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $url;
|
||||
@@ -668,7 +689,6 @@ class Curl extends BaseCurl
|
||||
}
|
||||
$this->setUrl($url, $data);
|
||||
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'OPTIONS');
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -679,6 +699,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function patch($url, $data = [])
|
||||
{
|
||||
$this->setPatch($url, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setPatch($url, $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $url;
|
||||
@@ -692,7 +718,6 @@ class Curl extends BaseCurl
|
||||
$this->setUrl($url);
|
||||
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
|
||||
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -722,6 +747,12 @@ class Curl extends BaseCurl
|
||||
* [3] http://php.net/ChangeLog-5.php#5.5.11
|
||||
*/
|
||||
public function post($url, $data = '', $follow_303_with_post = false)
|
||||
{
|
||||
$this->setPost($url, $data, $follow_303_with_post);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setPost($url, $data = '', $follow_303_with_post = false)
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$follow_303_with_post = (bool)$data;
|
||||
@@ -746,7 +777,6 @@ class Curl extends BaseCurl
|
||||
|
||||
$this->setOpt(CURLOPT_POST, true);
|
||||
$this->setOpt(CURLOPT_POSTFIELDS, $this->buildPostData($data));
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -757,6 +787,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function put($url, $data = [])
|
||||
{
|
||||
$this->setPut($url, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setPut($url, $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $url;
|
||||
@@ -773,7 +809,6 @@ class Curl extends BaseCurl
|
||||
if (!empty($put_data)) {
|
||||
$this->setOpt(CURLOPT_POSTFIELDS, $put_data);
|
||||
}
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -784,6 +819,12 @@ class Curl extends BaseCurl
|
||||
* @return mixed Returns the value provided by exec.
|
||||
*/
|
||||
public function search($url, $data = [])
|
||||
{
|
||||
$this->setSearch($url, $data);
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
public function setSearch($url, $data = [])
|
||||
{
|
||||
if (is_array($url)) {
|
||||
$data = $url;
|
||||
@@ -800,7 +841,6 @@ class Curl extends BaseCurl
|
||||
if (!empty($put_data)) {
|
||||
$this->setOpt(CURLOPT_POSTFIELDS, $put_data);
|
||||
}
|
||||
return $this->exec();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,13 +74,13 @@ export PHP_CURL_CLASS_TEST_MODE_ENABLED="yes"
|
||||
# Start test servers. Run servers on different ports to allow simultaneous
|
||||
# requests without blocking.
|
||||
server_count=7
|
||||
declare -A pids
|
||||
for i in $(seq 0 $(("${server_count}" - 1))); do
|
||||
pids=()
|
||||
for i in $(seq 0 "$(echo "${server_count} - 1" | bc)"); do
|
||||
port=8000
|
||||
(( port += $i ))
|
||||
|
||||
php -S "127.0.0.1:${port}" server.php &> /dev/null &
|
||||
pids["${i}"]="${!}"
|
||||
pids+=("${!}")
|
||||
done
|
||||
|
||||
# Determine which phpunit to use.
|
||||
|
||||
Reference in New Issue
Block a user