2013-10-10 149 views
0

需要在我的应用程序中请求两次代码。首先请求url作为ajax调用,并且还需要在控制器中请求这个url(类似于hmvc)。我知道如何通过curl来开发这个功能,但是我发现了另外一种想法,如何实现这个功能,只需使用函数file_get_contents以及之前准备好的参数。这是我的代码:无法通过file_get_contents在发布请求中发送数据

// Setup limit per page 
    $args['offset'] = $offset; 
    $args['limit'] = $this->_perpage; 
    // -- 

    // Convert search arguments to the uri format 
    $data = http_build_query($args); 

    // Define request params 
    $options = array(
     'http' => array(
      'header' => 'Content-type: application/json' . PHP_EOL . 
         'Content-Length: ' . strlen($data) . PHP_EOL, 
      'method' => 'POST', 
      'content' => $data, 
     ), 
    ); 

    $context = stream_context_create($options); 

    $result = file_get_contents(
     'http://'.$_SERVER['HTTP_HOST'].'/search/items', FALSE, $context 
    ); 

在请求的uri中检测到请求方法正常,但params未通过。为什么这不是传递参数要求?我的代码中的错误在哪里?非常感谢任何答案。

回答

1

内容类型应为application/x-www-form-urlencoded。如果您想留在application/json,请尝试使用file_get_contents("php://input")获取发布的数据。

+0

非常感谢,它的工作对我罚款。 –

相关问题