如何在远程服务器上阅读CURL POST?

时间:2012-10-15 23:24:25

标签: php curl

这是我的cURL POST函数:

public function curlPost($url, $data)
{
    $fields = '';

    foreach($data as $key => $value) { 
      $fields .= $key . '=' . $value . '&'; 
    }

    rtrim($fields, '&');

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $result = curl_exec($ch);
    $info   = curl_getinfo($ch);

    curl_close($ch);
}

$this->curlPost('remoteServer', array(data));

如何阅读远程服务器上的POST

远程服务器正在使用PHP ...但我应该阅读$_POST[]中的var

例如: - $_POST['fields']$_POST['result']

2 个答案:

答案 0 :(得分:0)

作为普通的POST请求...发布的所有数据都可以在$ _POST中找到...当然除了文件:)例如在网址中添加&action=request1

if ($_GET['action'] == 'request1') {

  print_r ($_POST);

}

编辑:要查看POST变量,请使用POST处理程序文件中的下列内容

if ($_GET['action'] == 'request1') {
  ob_start();
  print_r($_POST);
  $contents = ob_get_contents();
  ob_end_clean();
  error_log($contents, 3, 'log.txt' );
}

答案 1 :(得分:0)

您的代码有效,但我建议您添加其他两项内容

一个。由于HTTP 302

,因此CURLOPT_FOLLOWLOCATION
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

B中。 return如果您需要输出结果

return $result ;

实施例

function curlPost($url, $data) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    return $result;
}

print(curlPost("http://yahoo.com", array()));

另一个例子

    print(curlPost("http://your_SITE", array("greeting"=>"Hello World")));

要阅读您的帖子,您可以使用

 print($_REQUEST['greeting']);

 print($_POST['greeting']);