Skip to content

Commit

Permalink
Curl扩展支持 put/delete/patch 请求方式,并且开放request接口,和修复post请求
Browse files Browse the repository at this point in the history
  • Loading branch information
dogstar committed Jul 26, 2023
1 parent 69aa51d commit eb07226
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 17 deletions.
66 changes: 55 additions & 11 deletions src/CUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,54 @@ 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');
}

/**
* POST方式的请求
* @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');
}

/** ------------------ 前置方法 ------------------ **/
Expand Down Expand Up @@ -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);
Expand All @@ -189,17 +232,18 @@ 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;
$this->hascookie = FALSE;
unset($this->header['Cookie']);
unset($this->option[CURLOPT_COOKIEFILE]);
}

curl_close($ch);

return $rs;
Expand Down
2 changes: 1 addition & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* PhalApi框架 Kernal内核 版本号
*/
defined('PHALAPI_VERSION') || define('PHALAPI_VERSION', '2.21.6');
defined('PHALAPI_VERSION') || define('PHALAPI_VERSION', '2.22.0');

/**
* 考虑再三,出于人性化关怀,提供要些快速的函数和方法
Expand Down
48 changes: 43 additions & 5 deletions tests/src/CUrl_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -48,16 +50,52 @@ 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);

$this->assertTrue(is_string($rs));

}

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'));
Expand All @@ -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()
Expand Down

0 comments on commit eb07226

Please sign in to comment.