Skip to content

Commit

Permalink
Buggy
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilskalns committed Nov 2, 2017
1 parent eb0da32 commit 0e15bd0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,25 @@ $wcurl->setLogin( callback 'yourClass::cb_do_login' );
```
If any request results in HTTP 401 or 403 code, `wcurl` calls Login callback function and then repeats original request. If again there is error, it is returned to original function result. `wcurl` stores cookies in temporary file unique to API root. This cookie file is included in every request.

Callback must return true, if login success, otherwise it would fail to auto-repeat request after auth success.

**N!B! If login doesn't works, but still `return true`, it can cause `request->login->request->login...` infinitive loop**

Login function **example**
``` php
static function cb_do_login(){
$wcurl = \wcurl::instance();

$login = $wcurl->post("/login", array(
'login'=> 'my_user',
'password'=> 'covfefe' )
);
if($login['status']['http_code']==200){
return true;
}

// or
$wcurl->setHeaders( ['Authentication:xx-bla-bla-c0vfefe-secret'] );
}
```

Expand All @@ -58,22 +69,12 @@ Some session-less API's require user/api password in each request header. Others
``` php
$wcurl->setHeaders( array ['Accept:application/json, text/plain, */*'] );
```
or INI
``` ini
[wcurl]
headers = "Header: value", "Another-Header: Value"
```

#### Set User Agent string

``` php
$wcurl->setUserAgent( 'Zeus was here' );
```
or INI
``` ini
[wcurl]
useragent = Zeus was here
```

### How to use it

Expand Down Expand Up @@ -113,7 +114,7 @@ To use named route, pass it's name instead of full URL.
$response = $wcurl->get( 'allmembers' );
```

### Fill / URLs with variables variables
### Fill / URLs with variables

To fill existing URL either from your custom or trough `setRests()` available ones, pass `$key => $value` array as last parameter to method.

Expand All @@ -139,6 +140,8 @@ If you put all configuration in your main `ini` file, class can be initialized o
root=http://mysite.api/v1
ttl=3600
cb_login=yourClass::cb_do_login
useragent = Zeus was here
headers = "Header: value", "Another-Header: Value"

[wcurl.rests]
allmembers=/lists/members/all/pages
Expand All @@ -150,7 +153,7 @@ When calling `\wcurl::instance()` it is built like singleton class, thus in any
``` php
$apiTwo = new wcurl([$root] [,$cb_login] [,$ttl]);
```
And there `$apiTwo` can be stored in F3 hive.
And then `$apiTwo` can be stored in F3 hive.


There's a lot to improve, but currently will be making features I need. If something not possible for your use case, submit an issue or even PR.
Expand Down
30 changes: 15 additions & 15 deletions lib/wcurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class wcurl extends \Prefab {

private $cb_login, $root, $tll, $cookie, $headers, $ua;

private $version = 'v0.1';
private $version = 'v0.2';

private $rests = [];

Expand All @@ -23,17 +23,22 @@ function __construct($root = null, $cb_login = null, $ttl = null) {

if($cb_login){
$this->cb_login = $cb_login;
// call_user_func($cb_login);
}

if( !self::setTTL($ttl) && !self::setTTL($f3->get('wcurl.ttl')) ){
$this->ttl = 60;
}
if(is_array($f3->get('wcurl.rests')))
$f3->exists('wcurl.rests', $this->rests);
$this->rests = $f3->get('wcurl.rests');

if(is_array($f3->get('wcurl.headers')))
$f3->exists('wcurl.headers', $this->headers);
if( is_string($f3->get('wcurl.headers')) ){
$this->headers = [trim($f3->get('wcurl.headers'),'\'\"')];
} else if (is_array($f3->get('wcurl.headers'))){
foreach ($f3->get('wcurl.headers') as $head ) {
$this->headers[] = trim($head,'\'\"');
}
}
// $f3->exists('wcurl.headers', $this->headers);

$this->ua = 'f3-wcurl '.$this->version;
if(is_string($f3->get('wcurl.useragent')))
Expand Down Expand Up @@ -123,12 +128,12 @@ function get($url, $fill = null, $ttl = true){

function post($url, $body = null, $fill = null){


$url = self::fillRESTS($url, $fill);

if(is_array($body)){
$body = json_encode($body);
}

return $this->curl_send(array(
CURLOPT_URL => $url,
CURLOPT_POST => true,
Expand All @@ -138,6 +143,7 @@ function post($url, $body = null, $fill = null){

private function curl_send($params = array(), $nested = false){
global $f3;
set_time_limit(30);

$default = array(
CURLOPT_USERAGENT => $this->ua,
Expand All @@ -162,14 +168,10 @@ private function curl_send($params = array(), $nested = false){
curl_setopt_array($ch, $default);
curl_setopt_array($ch, $setparams);

if($this->headers){
if(is_array($this->headers)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
}
pre(json_encode($nested));
pre($url);
// pre(realpath($this->cookie));
// pre($this->headers);

$headers = [];
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
Expand All @@ -178,13 +180,11 @@ function($curl, $header) use (&$headers){
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;

$name = strtolower(trim($header[0]));
if (!array_key_exists($name, $headers))
$headers[$name] = [trim($header[1])];
else
$headers[$name][] = trim($header[1]);

return $len;
}
);
Expand All @@ -197,8 +197,8 @@ function($curl, $header) use (&$headers){
case 401:
case 403:
if($this->cb_login){
call_user_func($this->cb_login);
return $this->curl_send($params, true);
if(call_user_func($this->cb_login))
return $this->curl_send($params, true);
}
break;
}
Expand Down

0 comments on commit 0e15bd0

Please sign in to comment.