Php 使用curl发布json数据并失败

Php 使用curl发布json数据并失败,php,json,curl,Php,Json,Curl,我在这里有一个函数: public static function call($action, array $args) { $post_args = array( 'action' => $action, 'args' => $args ); $stream = json_encode($post_args); $headers = array(

我在这里有一个函数:

public static function call($action, array $args) 
{
  $post_args = array(
                'action'  => $action,
                'args'    => $args
               );
  $stream    = json_encode($post_args);
  $headers   = array(
                 'Content-type: application/json',
                 'Accept: application/json',
                 'Expect:'
               );
  $userpwd   = self::$_user.':'.sha1(sha1(self::$_pass));

  $ch   = curl_init();
  $args = array(
           CURLOPT_URL            => self::$_url,
           CURLOPT_FOLLOWLOCATION => TRUE,
           CURLOPT_RETURNTRANSFER => TRUE,
           CURLOPT_POST           => TRUE,
           CURLOPT_POSTFIELDS     => $stream,
           CURLOPT_HTTPHEADER     => $headers,
           CURLOPT_USERPWD        => $userpwd
          );
  curl_setopt_array($ch, $args);
  $res  = curl_exec($ch);
  $data = json_decode($res, true);
  if (isset($data) === TRUE
      && empty($data) === FALSE
  ) {
    $res = $data;
  }
  return $res;

}//end call()
在我发布的URL上,我只是在做:

echo file_get_contents('php://input');
但是什么也得不到,即使我发布数据。有什么问题吗?我走到了死胡同

另外,当我只是发布到本地机器上的一个简单虚拟主机URL,而不执行任何重定向时,为什么需要将CURLOPT_FOLLOWLOCATION设置为TRUE

编辑:

尝试使用fopen重做,如下所示:

public static function call($action, array $args) 
{
  $post_args = array(
                'action'  => $action,
                'args'    => $args
               );
  $stream    = json_encode($post_args);

  $userpwd   = self::$_user.':'.sha1(sha1(self::$_pass));

  $opts = array(
           'http' => array(
                      'method'  => 'POST',
                      'header'  => array(
                                     "Authorization: Basic ".base64_encode($userpwd),
                                     "Content-type: application/json"
                                   ),
                      'content' => $stream
                     )
          );

  $context = stream_context_create($opts);

  $res = '';
  $fp = fopen(self::$_url, 'r', false, $context);
  if($fp){
    while (!feof($fp)){
      $res .= fread($fp, 128);
    }
  }
  return $res;

}//end call()

没有成功。连接使用curl和fopen,因为我将状态与结果一起传递(这只是结果)php://input 流)。有什么想法吗?

您可以确定curl\u exec函数是否成功结束。还有,为什么不使用fopen来实现这个目的呢。我已经编写了一个JSON RPC客户端和服务器。我用fopen发送请求,它工作得很好

        $httpRequestOptions =
            array(
                'http'=>array(
                    'method'=>'POST',
                    'header'=>'Content-type: application/json',
                    'content'=>$requestJSON
                )
            );

        $context = stream_context_create($httpRequestOptions);

        // send request
        if($fileHandler = @fopen($serverURL, 'r', false, $context)){

剩下的我不写了。您可以使用我编写的代码。

发现了问题

我正在调用,因为我认为它会自动加载index.php,然后我将更改文件夹的默认文件名

问题是我没有在最后添加index.php——我应该调用它。 这样,它与cURL和fopen一起工作


知道如何在不暴露文件名的情况下调用API吗?

是的,我肯定curl\u exec会成功结束,因为我回显了一个json编码的数组,该数组也包含status integer,我得到了数组的这一部分。我将尝试用fopen重写函数。我用fopen重新编写了函数,但仍然无法获得包含文件内容的数据php://input'); 即使它读取了状态。只是为了确定,您正在向服务器URL部分写入URL,对吗?而不是文件路径。它必须类似于,
http://localhost/server.php
。如果您编写
server.php
或类似的东西,它就不起作用。发送的参数不会传递到
php://input
您的评论让我重新检查了我的URL。谢谢。把一个.htaccess文件放到你放php的文件夹中。在htaccess中写下这些:RewriteRule上的RewriteEngine.*myfile.php[PT,L]所有请求都转到myfile.php。似乎不起作用。我得到一个永久移动301响应,如果我将CURLOPT_FOLLOWLOCATION设置为true,我会得到一个空结果,就像以前一样。看看这个。可能会帮助您正确写入htaccess。也许你写错了。还有更多的事情,试着写一些东西,可能是日期时间,或者是你发送的请求,到php文件中的一个文件中。所以你可以理解它是正确工作的。