douben7493
2017-03-14 14:13
浏览 88
已采纳

使用cURL在CodeIgniter项目中调用控制器

Okay so this problem is bothering me quite a bit.

I've created a controller function in my CodeIgniter projects at [project_url]/admin/orderpicking/get_updated_statuses

function name: get_updated_statuses controller name: Orderpicking.php controller location: application/admin

Inside the controller all I have is

ob_start();
print_r('success');
file_put_contents('file.txt', ob_get_contents());
ob_end_clean();

to confirm that I am hitting the controller.

When I hit the controller directly from my browser, the file gets created with 'success' as the content.

When I use cURL from another project to hit the controller, nothing happens, the result is an empty string (I dont care about the result right now, but the file.txt file is no longer being created).

Code that calls the controller is as follows:

$data = array('datetime'=>new DateTime());
        $header = array(
            'Identification:Portal::ReadAPI', // TODO config
            'Content-Type:application/x-www-form-urlencode'
        );
        $url = '[local instance]/admin/orderpicking/get_updated_statuses'; // TODO config

        $curlGetUpdatedItems = curl_init($url);
        /** CURL OPTIONS */
        curl_setopt($curlGetUpdatedItems, CURLOPT_POST, 1);
        curl_setopt($curlGetUpdatedItems, CURLOPT_POSTFIELDS, json_encode(serialize($data)));
        curl_setopt($curlGetUpdatedItems, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curlGetUpdatedItems, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($curlGetUpdatedItems, CURLOPT_SSL_VERIFYPEER, true);
//        curl_setopt($curlGetUpdatedItems, CURLOPT_SSL_VERIFYHOST, 2);
//        curl_setopt($curlGetUpdatedItems, CURLOPT_CAINFO, $certificate_location);
        /** CURL OPTIONS */

        $result = curl_exec($curlGetUpdatedItems);

Are there any obvious mistakes I'm making here? I will add in a certificate later when I make the portal https but until then I don't think I need it?

Any help and/or advice is extremely welcome!

----- UPDATE -----

After var_dump'ing the result I've noticed that a boolean true is returned. curl_error($curlGetUpdatedItems) returns an empty string.

图片转代码服务由CSDN问答提供 功能建议

好的,这个问题让我很烦恼。

我' 我在我的CodeIgniter项目中创建了一个控制器函数[project_url] / admin / orderpicking / get_updated_statuses

函数名称:get_updated_statuses controller名称:Orderpicking.php controller location:application / admin

控制器内部只有

  ob_start(); 
print_r('success'); 
file_put_contents('file.txt'  ,ob_get_contents()); 
ob_end_clean(); 
   
 
 

确认我正在按下控制器。

当我 直接从我的浏览器点击控制器,文件以“成功”作为内容创建。

当我使用来自其他项目的cURL命中控制器时,没有任何反应,结果是 空字符串(我现在不关心结果,但不再创建file.txt文件)。

调用控制器的代码如下: \ n

  $ data = array('datet  ime'=> new DateTime()); 
 $ header = array(
'标识:Portal :: ReadAPI',// TODO config 
'Content-Type:application / x-www-form-urlencode'  
); 
 $ url ='[本地实例] / admin / orderpicking / get_updated_statuses';  // TODO config 
 
 $ curlGetUpdatedItems = curl_init($ url); 
 / ** CURL OPTIONS * / 
 curl_setopt($ curlGetUpdatedItems,CURLOPT_POST,1); 
 curl_setopt($ curlGetUpdatedItems,CURLOPT_POSTFIELDS,json_encode(serialize)  ($ data))); 
 curl_setopt($ curlGetUpdatedItems,CURLOPT_HTTPHEADER,$ header); 
 curl_setopt($ curlGetUpdatedItems,CURLOPT_RETURNTRANSFER,true); 
 // curl_setopt($ curlGetUpdatedItems,CURLOPT_SSL_VERIFYPEER,true); 
 //  curl_setopt($ curlGetUpdatedItems,CURLOPT_SSL_VERIFYHOST,2); 
 // curl_setopt($ curlGetUpdatedItems,CURLOPT_CAINFO,$ certificate_location); 
 / ** CURL OPTIONS * / 
 
 $ result = curl_exec($ curlGetUpdatedItems); 
 <  / code>  
 
 

我在这里有没有明显的错误? 我将在以后制作门户网站https时添加证书,但在此之前我认为我不需要它吗?

非常欢迎任何帮助和/或建议!

-----更新-----

在var_dump结果后我注意到了一个布尔值 返回true。 curl_error($ curlGetUpdatedItems)返回一个空字符串。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxiuyu2028 2017-03-14 16:28
    已采纳

    you can do it make by steps:

    $data_string = http_build_query($data);
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($data));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    
    $result = json_decode($result,true);
    

    don't know if need the header, i think that the default already is application/x-www-form-urlencode

    id it not works, try changing the $data values in array. think helps . :)

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题