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

使用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.

  • 写回答

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 . :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号