From eb07226d8b179228786476b655428e51faef7aec Mon Sep 17 00:00:00 2001 From: dogstar Date: Wed, 26 Jul 2023 16:10:17 +0800 Subject: [PATCH] =?UTF-8?q?Curl=E6=89=A9=E5=B1=95=E6=94=AF=E6=8C=81=20put/?= =?UTF-8?q?delete/patch=20=E8=AF=B7=E6=B1=82=E6=96=B9=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E4=B8=94=E5=BC=80=E6=94=BErequest=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E5=92=8C=E4=BF=AE=E5=A4=8Dpost=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CUrl.php | 66 ++++++++++++++++++++++++++++++++++------- src/functions.php | 2 +- tests/src/CUrl_Test.php | 48 ++++++++++++++++++++++++++---- 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/src/CUrl.php b/src/CUrl.php index 6782f6e..6eea139 100644 --- a/src/CUrl.php +++ b/src/CUrl.php @@ -64,10 +64,10 @@ public function __construct($retryTimes = 1) { * GET方式的请求 * @param string $url 请求的链接 * @param int $timeoutMs 超时设置,单位:毫秒 - * @return string 接口返回的内容,超时返回false + * @return string|boolean 接口返回的内容,超时返回false */ public function get($url, $timeoutMs = 3000) { - return $this->request($url, array(), $timeoutMs); + return $this->request($url, array(), $timeoutMs, 'GET'); } /** @@ -75,10 +75,43 @@ public function get($url, $timeoutMs = 3000) { * @param string $url 请求的链接 * @param array $data POST的数据 * @param int $timeoutMs 超时设置,单位:毫秒 - * @return string 接口返回的内容,超时返回false + * @return string|boolean 接口返回的内容,超时返回false */ public function post($url, $data, $timeoutMs = 3000) { - return $this->request($url, $data, $timeoutMs); + return $this->request($url, $data, $timeoutMs, 'POST'); + } + + /** + * PUT方式的请求 + * @param string $url 请求的链接 + * @param array $data PUT的数据 + * @param int $timeoutMs 超时设置,单位:毫秒 + * @return string|boolean 接口返回的内容,超时返回false + */ + public function put($url, $data, $timeoutMs = 3000) { + return $this->request($url, $data, $timeoutMs, 'PUT'); + } + + /** + * DELETE方式的请求 + * @param string $url 请求的链接 + * @param array $data DELETE的数据 + * @param int $timeoutMs 超时设置,单位:毫秒 + * @return string|boolean 接口返回的内容,超时返回false + */ + public function delete($url, $data, $timeoutMs = 3000) { + return $this->request($url, $data, $timeoutMs, 'DELETE'); + } + + /** + * PATCH方式的请求 + * @param string $url 请求的链接 + * @param array $data PATCH的数据 + * @param int $timeoutMs 超时设置,单位:毫秒 + * @return string|boolean 接口返回的内容,超时返回false + */ + public function patch($url, $data, $timeoutMs = 3000) { + return $this->request($url, $data, $timeoutMs, 'PATCH'); } /** ------------------ 前置方法 ------------------ **/ @@ -159,26 +192,36 @@ public function withCookies() { * @param string $url 请求的链接 * @param array $data POST的数据 * @param int $timeoutMs 超时设置,单位:毫秒 - * @return string 接口返回的内容,超时返回false,异常取消抛出时返回NULL + * @param string $requestMethod 请求方式,例如:GET/POST/PUT/DELETE,不确定服务器支持这个自定义方法则不要使用它。 + * @return string 接口返回的内容,超时返回false,异常取消抛出时返回NULL * @throws Exception */ - protected function request($url, $data, $timeoutMs = 3000) { + public function request($url, $data, $timeoutMs = 3000, $requestMethod = NULL) { $rs = NULL; $options = array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HEADER => 0, + CURLOPT_TIMEOUT_MS => $timeoutMs, CURLOPT_CONNECTTIMEOUT_MS => $timeoutMs, CURLOPT_HTTPHEADER => $this->getHeaders(), ); + // 请求方式 + $requestMethod = strtoupper($requestMethod); + if ($requestMethod) { + $options[CURLOPT_CUSTOMREQUEST] = $requestMethod; + } + if ($requestMethod == 'POST') { + $options[CURLOPT_POST] = TRUE; + } + if (!empty($data)) { - $options[CURLOPT_POST] = 1; - $options[CURLOPT_POSTFIELDS] = $data; + $options[CURLOPT_POSTFIELDS] = $data; } - $options = $this->option + $options; //$this->>option优先 + $options = $this->option + $options; // $this->>option优先 $ch = curl_init(); curl_setopt_array($ch, $options); @@ -189,10 +232,10 @@ protected function request($url, $data, $timeoutMs = 3000) { } while ($rs === FALSE && $curRetryTimes >= 0); $errno = curl_errno($ch); if ($errno && $this->isThrowExcption) { - throw new InternalServerErrorException(sprintf("%s::%s(%d)\n", $url, curl_error($ch), $errno)); + throw new InternalServerErrorException(sprintf("%s %s (Curl error: %d)\n", $url, curl_error($ch), $errno)); } - //update cookie + // update cookie if ($this->hascookie) { $cookie = $this->getRetCookie(curl_getinfo($ch, CURLINFO_COOKIELIST)); !empty($cookie) && $this->cookie = $cookie + $this->cookie; @@ -200,6 +243,7 @@ protected function request($url, $data, $timeoutMs = 3000) { unset($this->header['Cookie']); unset($this->option[CURLOPT_COOKIEFILE]); } + curl_close($ch); return $rs; diff --git a/src/functions.php b/src/functions.php index a9ff4e6..eed811d 100644 --- a/src/functions.php +++ b/src/functions.php @@ -7,7 +7,7 @@ /** * PhalApi框架 Kernal内核 版本号 */ -defined('PHALAPI_VERSION') || define('PHALAPI_VERSION', '2.21.6'); +defined('PHALAPI_VERSION') || define('PHALAPI_VERSION', '2.22.0'); /** * 考虑再三,出于人性化关怀,提供要些快速的函数和方法 diff --git a/tests/src/CUrl_Test.php b/tests/src/CUrl_Test.php index 7249569..76d733f 100644 --- a/tests/src/CUrl_Test.php +++ b/tests/src/CUrl_Test.php @@ -16,6 +16,8 @@ class PhpUnderControl_PhalApiCUrl_Test extends \PHPUnit_Framework_TestCase { public $curl; + public $apiHost = 'http://demo.phalapi.net'; + protected function setUp() { parent::setUp(); @@ -33,8 +35,8 @@ protected function tearDown() */ public function testGet() { - $url = 'http://demo.phalapi.net/?s=App.Hello.World'; - $timeoutMs = 3000; + $url = $this->apiHost . '/?s=App.Hello.World'; + $timeoutMs = 30000; $rs = $this->curl->get($url, $timeoutMs); //var_dump($rs); @@ -48,9 +50,9 @@ public function testGet() */ public function testPost() { - $url = 'http://demo.phalapi.net/'; + $url = $this->apiHost . '/'; $data = array('username' => 'phalapi'); - $timeoutMs = 3000; + $timeoutMs = 30000; $rs = $this->curl->post($url, $data, $timeoutMs); @@ -58,6 +60,42 @@ public function testPost() } + public function testPut() + { + $url = $this->apiHost . '/'; + $data = array('username' => 'phalapi'); + $timeoutMs = 30000; + + $rs = $this->curl->put($url, $data, $timeoutMs); + + $this->assertTrue(is_string($rs)); + + } + + public function testDelete() + { + $url = $this->apiHost . '/'; + $data = array('username' => 'phalapi'); + $timeoutMs = 30000; + + $rs = $this->curl->delete($url, $data, $timeoutMs); + + $this->assertTrue(is_string($rs)); + + } + + public function testPatch() + { + $url = $this->apiHost . '/'; + $data = array('username' => 'phalapi'); + $timeoutMs = 30000; + + $rs = $this->curl->patch($url, $data, $timeoutMs); + + $this->assertTrue(is_string($rs)); + + } + public function testSet() { $this->curl->setHeader(array('Content-Type' => 'text')); @@ -71,7 +109,7 @@ public function testCookie() $this->curl->withCookies(); - $rs = $this->curl->get('http://demo.phalapi.net/', 3000); + $rs = $this->curl->get('http://demo.phalapi.net/', 30000); } public function testGetRetCookie()