【问题标题】:Post data with curl to another server使用 curl 将数据发布到另一台服务器
【发布时间】:2014-04-10 10:41:41
【问题描述】:

我尝试使用 curl 将一些数据发布到另一台服务器。问题是我在另一台服务器上什么也得不到。

服务器1:

$a = $USER->id;
$b = $USER->username;

 error_reporting(-1);
    $url = 'http://remote_server/a.php';
$data = array(
   'cus' => $a,
   'cust' => $b
);
    $postString = http_build_query($data, '', '&');
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_POST, 2);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postString);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    $post = curl_exec ($ch);
    curl_close ($ch);

在 Server2 上:

<?php
session_start();
var_dump($_POST);
?>

当然不行,它给出了array(0) {}。 那么,我将如何查看其他服务器上的数据?

【问题讨论】:

  • 你能回显 $postString 吗?
  • 是的,我在 Server1 上添加了“echo”,它给了我:cus=2&cust=admin。这两个是我从数据库中获取的ID和用户名。

标签: php post curl


【解决方案1】:

要在 Server2 中查看 var_dump($_POST) 的实际输出,您必须:

  • Server1 端:echo $post 变量
  • Server2 端:将 var_dump 的结果内容写入文件:

    ob_start();
    var_dump($_POST);
    $output = ob_get_clean();
    $fp = fopen('log.txt', 'a');
    fwrite($fp, $output);
    fclose($fp);
    

【讨论】:

  • 感谢您的评论,我已经按照您的建议做了,但没有成功:文件创建成功但为空..
  • 不能为空。它必须至少有array(0) {}。请提供 server2 端的完整代码。
  • 是的,没错,它只是数组 (0)。我只是使用了你提供的代码而已。
  • 您确定array(0) {} 行是在 Server1 上执行脚本的结果吗?看来您是通过 GET 请求直接调用 Server2...
猜你喜欢
  • 2023-03-09
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
  • 2013-12-03
  • 2015-08-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多