\PhalApi_CUrl

CUrl Request Class

implement simple class to request API with curl


Usage:

 // retry 2 times when fail
 $curl = new PhalApi_CUrl(2);

 // GET
 $rs = $curl->get('http://demo.phalapi.net/?service=Default.Index');

 // POST
 $data = array('username' => 'dogstar');
 $rs = $curl->post('http://demo.phalapi.net/?service=Default.Index', $data);

Summary

Methods
Properties
Constants
setHeader()
setOption()
__construct()
get()
post()
No public properties found
MAX_RETRY_TIMES
getHeaders()
request()
$retryTimes
$header
$option
N/A
No private methods found
No private properties found
N/A

Constants

MAX_RETRY_TIMES

MAX_RETRY_TIMES

Retry no more than 10 times.

Properties

$retryTimes

$retryTimes : integer

Type

integer — retry times; NOTE, total request times = 1 + retry times

$header

$header : 

Type

$option

$option : 

Type

Methods

setHeader()

setHeader(array  $header) : $this

Set curl header

Parameters

array $header

key-value pair like:

array(
    ['Accept' => 'text/html'],
    ['Connection' => 'keep-alive'],
)

Returns

$this

setOption()

setOption(array  $option) : $this

Set curl option

  • 1、后设置的会覆盖之前的设置
  • 2、开发者设置的会覆盖框架的设置

Parameters

array $option

key-value pair

Returns

$this

__construct()

__construct(integer  $retryTimes = 1) 

Parameters

integer $retryTimes

retry times, default is 1

get()

get(string  $url, integer  $timeoutMs = 3000) : string

Request by GET

Parameters

string $url

the url wait to request

integer $timeoutMs

timeout, unit: microsecond

Returns

string —

response content, return false when time out or fail to connect

post()

post(string  $url, array  $data, integer  $timeoutMs = 3000) : string

Request by POST

Parameters

string $url

the url wait to request

array $data

POST data

integer $timeoutMs

timeout, unit: microsecond

Returns

string —

response content, return false when time out or fail to connect

getHeaders()

getHeaders() : array

Returns

array

request()

request(string  $url, array  $data, integer  $timeoutMs = 3000) : string

Request implementation

Parameters

string $url

the url wait to request

array $data

POST data

integer $timeoutMs

timeout, unit: microsecond

Returns

string —

response content, return false when time out or fail to connect