404 Not Found
404 Not Found

Reputation: 1223

curl is not working in Yii

when i am using curl in my core php file it's working fine for me and getting expected result also... my core php code is...

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, "http://stage.auth.stunnerweb.com/index.php?r=site/getUser");
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $data = curl_exec($curl);
  echo $data; //here i am getting respond proper

here in above i am making call to getUser function and i am getting respond from that function...

but now my problem is when i am using this same code in my any Yii controller (tried to use it in SiteController & Controller) but it's not working...

    public function beforeAction()
    {
        if(!Yii::app()->user->isGuest)
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL ,"http://stage.auth.stunnerweb.com/index.php?r=site/kalpit");
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            $data = curl_exec($curl);
            echo $data;
        }
        else
            return true;
    }

in yii can't we use curl like this?

Can you please suggest me how to use curl in yii?

Thanks in advance

Upvotes: 0

Views: 5562

Answers (2)

DaSourcerer
DaSourcerer

Reputation: 6606

You are running your code inside a beforeAction() method which is not supposed to render any data at all. On top of that, you do not let the method return anything if the current user is a guest. Please read the API docs concerning this.

Upvotes: 0

Harikrishnan
Harikrishnan

Reputation: 9979

Better use yii-curl

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

Upvotes: 1

Related Questions