Yii 1.1: yii-curl

curl wrapper for yii
20 followers

Curl Wrapper for Yii framework

GitHub url

Yii Curl extension on github (https://github.com/hackerone/curl/)

please download the file from github.

Requirements

  • PHP 5.2+
  • Yii 1.1.7 (should work on older versions too)
  • Curl and php-curl installed

Setup instructions

  • Place Curl.php into protected/extensions folder of your project
  • in main.php, add the following to 'components':
php
    'curl' => array(
            'class' => 'ext.Curl',
            'options' => array(/.. additional curl options ../)
        );

Usage

  • to GET a page with default params
php
    $output = Yii::app()->curl->get($url, $params);
    // output will contain the result of the query
    // $params - query that'll be appended to the url
  • to POST data to a page
php
    $output = Yii::app()->curl->post($url, $data);
    // $data - data that will be POSTed

  • to PUT data
php
    $output = Yii::app()->curl->put($url, $data, $params);
    // $data - data that will be sent in the body of the PUT

  • to set options before GET or POST or PUT
php
    $output = Yii::app()->curl->setOption($name, $value)->get($url, $params);
    // $name & $value - CURL options
    $output = Yii::app()->curl->setOptions(array($name => $value))->get($get, $params);
    // pass key value pairs containing the CURL options

changelog

v 1.2 * added PUT option

Total 7 comments

#16399 report it
once at 2014/02/19 07:01am
would be greate if you implement multicurl feature

would be greate if you implement multicurl feature

somethink like that

$output = Yii::app()->curl->multi()->get(array( 'http://google.com', 'http://yahoo.com' ), $params, function ($url, [result]) { // callback called when any url is loaded });

$output = Yii::app()->curl->multi()->post(array( 'http://google.com', 'http://yahoo.com' ), $params, function ($url, [result], something else) {

});

#12390 report it
edwaa at 2013/03/18 03:55am
Not sure this is correct but it works for me

Changes line 95-97:

Yii::app()->onEndRequest = function() use(&$ch) {
     curl_close($ch);
};

To:

Yii::app()->onEndRequest=array($this,'closeCurl');

And add this new method to the class:

public function closeCurl() {
     curl_close($this->_ch);
}
#12389 report it
edwaa at 2013/03/18 03:26am
This doesn't work with PHP 5.2.9

I get the following error when I try to use this with PHP 5.2.9:

Php Parse Error: syntax error, unexpected T_FUNCTION in /path/to/file/Curl.php on line 95

Can the code be modified to work with PHP 5.2? I'm not familiar with anonymous functions to know how to fix this. Thanks for your help!

#11710 report it
hackerone at 2013/01/29 02:41am
http header

my bad..

the http_header should not be an associative array. what you're doing is correct.

#11709 report it
AustinGeek at 2013/01/28 07:13pm
HTTP HEADER

Thank you Hackerone.

I had some trouble with this.

I tried to set the header in your example. It wouldn't connect. I get a response of "Not Authorized" $jsonObject = Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, array("Authorization" => $token ))->get($url);

Used this instead $auth = array('Authorization:'.$token);

$output = Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, $auth)->get($url);

That worked !

Thanks so much for your help and the extension!

#11690 report it
hackerone at 2013/01/27 04:46pm
use setOption or setOptions

Hi Austin,

you can setOption before getting the url

Yii::app()->curl->setOption(CURLOPT_COOKIE, 'cookie_one=value; cookie_two=value')->get(url);

or

Yii::app()->curl->setOptions(array(CURLOPT_COOKIE => 'cookie_one=value; cookie_two=value'))->get(url);

same can be done with CURLOPT_HTTPHEADER

setOption(CURLOPT_HTTPHEADER, array('Authorization' => '12345'))

#11646 report it
AustinGeek at 2013/01/24 11:27am
How to use cookies and send info on the curl command

I was trying to use this extension. I created a json string and posted it to authorize a connection. That worked.

I need to send a token with the GET command but not sure how to do it. curl -H "Authorization:12132455dae234234" 'http://api.site.com/getdata'

I could also use a cookie but not sure how to set it up. curl -b cookie 'http://api.site.com/getdata' ;

The way parameters work in the get() they are a suffix to the url. get(url,params)

Any suggestions? Thanks !

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet