PHP - file_get_contents将永远失败并失败

时间:2014-09-07 20:08:26

标签: php zend-framework

我有这个ZF控制器,它是一个非常基本的数据库查询,并将用户状态读为true或false。如果我在浏览器中使用直接URL,它会立即生效。

服务器1:

  public function getstatusAction() {
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');       
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();   
    $post = $this->getRequest()->getQuery();
    //$data = (object) $post;
    $this->db = Application_Model_Db::db_load();

    $sql = "select id from users where
              status='online'
        limit 1";
    $result = $this->db->fetchAll($sql);

    $json = '';
    if (count($result) > 0) {
      $json = array(
          'result' =>true,
      );      
    }   else {
      $json = array(
          'result' =>false,
      );        
    } 


    $this->getResponse()
            ->setHeader('Content-Type', 'application/json')
            ->setBody(Zend_Json::encode($json))
            ->sendResponse();

    exit;
  } 

但是当我从服务器2读取该网址使用以下内容时,它会永远消失,甚至无法获得成功

$section = file_get_contents("http://server1.domain.com/ajax/getstatus");

我该如何解决? (也没有看到任何错误日志)

1 个答案:

答案 0 :(得分:1)

确保服务器2上的allow_url_fopen设置为“1”。

或者您也可以使用此功能:

function get_content($URL){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_URL, $URL);
      $data = curl_exec($ch);
      curl_close($ch);
      return $data;
  }
相关问题