【问题标题】:posting json data with curl and failing使用 curl 发布 json 数据并失败
【发布时间】:2012-01-03 20:24:20
【问题描述】:

我这里有这个功能:

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 流)。有什么想法吗?

【问题讨论】:

    标签: php json curl


    【解决方案1】:

    你能确定 curl_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)){
    

    剩下的我就不写了。你可以使用我写的这段代码。

    【讨论】:

    • 是的,我确信 curl_exec 成功结束,因为我正在回显一个也包含状态整数的 json 编码数组,并且我得到了数组的那一部分。我会尝试用 fopen 重写函数。
    • 我用 fopen 重新做了,但仍然没有运气用 file_get_contents('php://input');即使它读取了状态。
    • 只是为了确定这一点,您正在将 URL 写入 serverURL 部分,对吗?而不是文件路径。它必须类似于http://localhost/server.php。如果你写server.php 或类似的东西,它就不起作用。发送的参数不传递给php://input
    • 您的评论让我重新检查了我的网址。谢谢。
    【解决方案2】:

    发现问题。

    我打电话给http://localhost/api,因为我认为它会自动加载index.php,然后我打算更改文件夹的默认文件名。

    问题是我没有在最后添加 index.php - 我应该调用 http://localhost/api/index.php。 这样它就可以与 cURL 和 fopen 一起使用。

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

    【讨论】:

    • 将 .htaccess 文件放入您放置 php.ini 文件的文件夹中。在 htaccess 中写下这些:RewriteEngine On RewriteRule .* myfile.php [PT,L] 所有请求都转到 myfile.php。
    • 似乎不起作用。我收到 Moved Permanently 301 响应,如果我将 CURLOPT_FOLLOWLOCATION 设置为 true,我会像以前一样得到一个空结果。
    • noupe.com/php/10-mod_rewrite-rules-you-should-know.html 看看这个。可以帮助您正确编写 htaccess。也许你写错了。更重要的是,尝试将一些内容(可能是日期时间,或者您发送的请求)写入 php 文件中的文件。所以你可以理解它正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 2013-09-09
    • 2012-06-20
    相关资源
    最近更新 更多