【发布时间】:2015-07-02 11:20:03
【问题描述】:
所以我的一个 php 文件中有一些变量,我想将这些变量数据发送到另一个域。所以我使用了 cURL 函数。
<?php
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
echo $result;
curl_close($post);
}
$data = array(
"name" => "c.bavota",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan"
);
post_to_url("http://anotherdomain.com/receive.php", $data);
//header("location: http://anotherdomain.com/receive.php");
?>
我的 receive.php 文件的内容是-
<?php
echo $_POST["name"];
?>
然后我得到一个空白页。 我哪里错了?还有另一种方法可以在接收端获取结果吗? 此外,当我尝试使用理想的 receive.php 进行跟踪时,我只得到空白页。
post_to_url("http://samedomain.com/receive.php", $data);
【问题讨论】:
-
添加了“PHP”标签。请永远不要忘记这样做。
标签: php post curl cross-site