Curl Wrapper

Hi guys,

just made this simple Curl wrapper for yii. i’ve seen one in extensions earlier, but wanted one which is light weight and simpler to use for my project, so made this one.

it’s on github

hackerone/curl

thanks for your feedbacks in advance :)

Seems very easy to use. Thanks for sharing it!

I seem to be having trouble with a simple example.

I get

"Use of undefined constant CURLOPT_RETURNTRANSFER - assumed ‘CURLOPT_RETURNTRANSFER’ "

when executing

$output = Yii::app()->curl->init();

I looked at the package, it seems pretty simple. I can execute curl calls from the command line but it seems

that my package is not configured or something of the sort.

My main.php in config is set like this. Not sure what options I need to set…the init() should be setting them.




'curl' => array(

			'class' => 'ext.Curl',

			//'options' => array(/.. additional curl options ../)

		);



The examples with the yii-curl wrapper were pretty simplistic so maybe I misinterpreted them.

I think your PHP instance doesn’t have CURL support.

Is it listed in modules?

If you run phpinfo() on your server you should see if it does or not.

You are probably correct.

I’m on Ubuntu 12.04LTS / NGINX / PHP5 / MySQL

I did do an install php5-curl and it seemed to work cambe back with setting up php5-curl (so it wasn’t installed before)

I have been using curl from the command line recently in preparation for this project.

I don’t see it when I issue phpinfo()…it comes back with alot of stuff, would curl be in it’s own block

right after "core" parameters?

It should be in it’s own block. :)

You probably need to enable the module in php.ini.

I am looking at php.ini but don’t see anything about curl…

php_curl, I guess.

Jacmoe,

You were correct.

php_curl needed to be installed, it gave me a bit of difficulty but I think the problem was having to reboot the server

before the module showed up…Maybe I just needed to restart the webserver…

Works like a charm, thanks again for your help!

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 !

This worked for me…




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

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



Looking to expand my use of Curl

I have a need to request a file download from a site. Normally I request a json object, that works great.

The curl command from the command line in linux generates the request and streams the data via redirect to a file as follows

curl -b cookies http://website.com/download?id=123231’ > foo.csv

How can I do this with this extension?

I use the CURLOPT_HTTPHEADER to set the autherization instead of using cookies.

Just not sure how to redirect the stream to a file.

Anyone have any suggestions ?

Thanks in Advance !

PUT not working…

I am trying to emulate curl -X PUT -d @datahttp://someurl?params

I implemented this in a model.




        $token = $this->openConnection();

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


	$api = 'publisher';

	$url = $this->base_uri.'/'.$api.'?publisher_id=' ;


	$data = '{ "publisher" : { "id" : "'.$pub.'", "base_payement_rule_id": '.$pay.'}}';


	$jsonObject = Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, $auth)->put($url, $data);

	$obj = json_decode($jsonObject);;



I received an error message of parameter 2 must be an array, thought it could work like POST so I modified the PUT Curl.php.

Curl.php




public function put($url, $data = array(), $params = array()){		

        	$this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT');

		$this->setOption(CURLOPT_POSTFIELDS, $data);

		return $this->_exec($this->buildUrl($url, $params));

	}


//	public function put($url, $params = array()){		

//	$this->setOption(CURLOPT_CUSTOMREQUEST, 'PUT');

//		return $this->_exec($this->buildUrl($url, $params));

//	}

Changed model to this:




        $token = $this->openConnection();

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


	$api = 'publisher';

	$url = $this->base_uri.'/'.$api ;


	$data = '{ "publisher" : { "id" : "'.$pub.'", "base_payement_rule_id": '.$pay.'}}';


	$params = array("publisher_id" => $pub);


	$jsonObject = Yii::app()->curl->setOption(CURLOPT_HTTPHEADER, $auth)->put($url, $data, $params);

	$obj = json_decode($jsonObject);;



Am I missing something? My version works, but I have to believe I was using the original incorrectly.

Anyone?

Thanks!