卷曲在Yii中不起作用

时间:2013-11-21 06:10:29

标签: php curl yii

当我在我的核心php文件中使用curl时,它对我来说工作正常并得到预期结果...我的核心PHP代码是...

  $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

在上面我正在调用 getUser 函数,我正在接受该函数的响应......

但现在我的问题是当我在任何 Yii控制器中使用相同的代码时(试图在SiteController和amp; Controller中使用它)但是它不起作用...

    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;
    }
在yii中

我们不能像这样使用curl吗?

你能否建议我如何在yii中使用curl?

提前致谢

2 个答案:

答案 0 :(得分:0)

更好地使用yii-curl

设置说明

将Curl.php放入项目的protected / extensions文件夹中 在main.php中,将以下内容添加到'components': 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

到PUT数据 PHP

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

在GET或POST或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

答案 1 :(得分:0)

您正在beforeAction()方法中运行代码,该方法根本不应该呈现任何数据。最重要的是,如果当前用户是访客,则不要让方法返回任何。请阅读有关此内容的API docs