Curl
Create cCurl calls
Create cCurl calls
$params - array Inititial parameters
$data = $this->get('curl') ->setUrl($url) ->setData($data) ->getResponse();
Set initial values from account preferences:
### Preferences
webservice:
service_name:
protocol: 'https'
host: 'app.ekansio.com'
username: _username
password: _password
headers:
Content-Type: 'application/json'
options:
CURLOPT_RETURNTRANSFER: 1
data:
always: 'ultra'
Create Curl object:
$Curl = $this->get('curl','service_name');
Set values as array: (preferred)
$Curl = $this->get('curl',Array(
'protocol' => 'https',
'host' => 'app.ekansio.com',
'username' => $username,
'password' => $password
));
Or using methods: (for additional values)
$Curl = $this->get('curl')
->setHost('app.ekansio.com');
$Curl = $this->get('curl','service_name')
->setMethod('POST')
->setHeaders(array(
'Content-Type' => 'application/json'
))
->setUrl('{protocol}://{username}:{password()}@{host}')
->setResource('users')
->setOptions(Array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10
))
->setData(Array(
'name' => $value
));
// OR set entire url directly (does not add resource)
$Curl->setUrl("{$Curl->getProtocol()}://{$Curl->getUsername()}:{$Curl->getPassword()}@{$Curl->getHost()}"/user");
Fetch data
if ( $Curl->isSuccess() ){
$data = $Curl->getResponseArray();
}
else {
print "Error ".$Curl->getErrorNumber().": ".$Curl->getErrorMessage();
}
Curl parameters | Return | Description |
---|---|---|
getCurl() | handle | Return cCurl handle |
setMethod() | string | Set httpMethod. GET, POST, DELETE, PATCH, PUT. (Will set data accordingly) |
setHeaders($array) | this | Set headers as assoc array. |
setOption($name,$value) | this | Set option. |
setOptions($array) | this | Add options as assoc array. |
setUrlTemplate(string) | this | Set string that url is generated from. Replaces {host} with getHost() etc. |
setUrl(string) | this | Set actual URL. |
setResource(string) | this | Set resource path. Is added to url with slash |
setResource(string) | this | Set resource path |
getMethod() | string | Get httpMethod. |
getHeaders() | array | Get all headers. |
getResource() | string | Get resource path |
getUrl() | string | Get actual URL. |
Additional parameters | Return | Description |
---|---|---|
setProtocol() | this | Set protocol. |
setHost() | this | Set host. |
setPassword() | this | Set password. |
setUsername() | this | Set Username. |
setMyparameter() | this | Set any custom parameter. |
getProtocol() | this | Get protocol. |
getHost() | this | Get host. |
getPassword() | this | Get password. |
getUsername() | this | Get Username. |
getMyparameter() | this | Get any custom parameter. |
All these methods run execute(), if not yet executed.
Get response | Return | Description |
---|---|---|
execute() | response | Execute curl call. |
getResponseContent() | mixed | Actual curl return value |
getResponse([$resource,$data]) | string | Response content. setSErvice() and setData() if given. |
getResponseArray() | array | Response as array. |
getResponseHeaders() | array | Response headers. |
Error handling | Return | Description |
---|---|---|
isSuccess() | bool | REturn TRUE, if no errors. |
getStatus() | array | Response headers. 200 if OK. |
getErrorNumber() | int | Curl error number |
getErrorMessage() | string | Error text (From curl or service) |