【问题标题】:Get json data to the outside of codeigniter script获取 json 数据到 codeigniter 脚本的外部
【发布时间】:2017-07-08 19:47:47
【问题描述】:

我正在使用 codeigniter 3.1.1 。我正在尝试将 json 数据放到 codeigniter 脚本之外。

Codeigniter Valid.php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Valid extends CI_Controller 
{

    public function __construct() 
    {
        parent::__construct();
        $this->load->model("user_model");

    }

    public function index() 
    {

    if($this->user->loggedin) {
        $json['success'] = "false"; 
    } else {
        $json['success'] = "true"; 
    }


   echo json_encode($json);

  }

}

Codeigniter Check.php 之外

$url = 'http://url.com/valid';

  $curl_handle = curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,$url);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,TRUE);
  curl_setopt($curl_handle, CURLOPT_HEADER, FALSE);
  curl_setopt ($curl_handle,CURLOPT_CONNECTTIMEOUT,0);
  curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, TRUE);
  $json = curl_exec($curl_handle);
  curl_close($curl_handle);

    $json = json_decode($json);
    echo $json['success'];

我每次都收到$json['success'] = false

【问题讨论】:

    标签: php json codeigniter


    【解决方案1】:

    像这样试试..

    1.这里$this->user->loggedin不正确。我认为需要调用user_modellogged_in()函数。所以

    public function index() 
        {
        $this->load->library('user');//loads user library
        if($this->user->loggedin()) {
            $json['success'] = "false"; 
        } else {
            $json['success'] = "true"; 
        }
    

    2.在Check.php中。您必须将true参数与json_decode()一起传递才能转换为数组。

    $json = json_decode($json,true);//converts into array
    echo $json['success'];
    

    【讨论】:

    • 我在 user_model 中没有 loggedin() 函数。 loggedin() 包含在 libraries 中。
    • 你在哪里加载库?
    • 也使用$this->load->library('library_name');加载库。我已经编辑了答案。
    • application/libraries.
    • 不是我仍然是假的。当我浏览 http://localhost/valid 时,我得到结果 {"success":"true"} ,当我浏览 Codeigniter 之外的 http://localhost/test/check 时,我的意思是在根目录的子文件夹中,然后我得到 {"success":"false"}
    猜你喜欢
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2017-12-01
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多