Curl Wrapper for Yii framework
Yii Curl extension on github (https://github.com/hackerone/curl/)
please download the file from github.
php
'curl' => array(
'class' => 'ext.Curl',
'options' => array(/.. additional curl options ../)
);
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
php
$output = Yii::app()->curl->post($url, $data);
// $data - data that will be POSTed
php
$output = Yii::app()->curl->put($url, $data, $params);
// $data - data that will be sent in the body of the 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
v 1.2 * added PUT option
Total 6 comments
Changes line 95-97:
To:
And add this new method to the class:
I get the following error when I try to use this with PHP 5.2.9:
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!
my bad..
the http_header should not be an associative array. what you're doing is correct.
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!
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'))
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 login to leave your comment.