我正在使用Codeigniter Framework,我想做这样的事情:
我希望从服务器1向服务器2发送$_GET
varibale,如下所示:www.server1.com?foo=123
现在在服务器2上,检查$_GET==123
是否返回了一些数据。
我的代码如下:
服务器1上的,例如:www.server1.com?foo=hello
if(isset($_GET['foo'])){
$post_fields = array(
'foo' => $_GET['foo']
);
$ch = curl_init('http://server2.com/master.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
die($result);
}
,服务器2上的代码如下所示:
$variable = $_POST['foo'];
if($variable=="hellow"){
echo "right!";
}else{
echo "wrong";
}
当我运行此代码时,我收到了400个错误请求 - nginx:
答案 0 :(得分:2)
这有效:
服务器1:
<?php
// for debug purposes removed it on production
error_reporting(E_ALL);
ini_set('display_errors', 1);
// end debug
if(isset($_GET['foo'])){
$post_fields = array(
'foo' => $_GET['foo']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://server2.com/master.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);
if ($result == "OK"){
echo "Post OK";
}else{
echo "Post NOT OK";
}
}else{
die("GET NOT Received");
}
?>
<强>服务器2:强>
<?php
// for debug purposes removed it on production
error_reporting(E_ALL);
ini_set('display_errors', 1);
// end debug
if(isset($_POST['foo'])){
$variable = $_POST['foo'];
if($variable=="hello"){
echo "right!";
}else{
echo "wrong";
}
}else{
die("POST NOT Received");
}
?>
答案 1 :(得分:0)
尝试使用类似的东西可能有帮助
$url = 'http://server2.com/master.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_POST, 1);
echo $ret = curl_exec($ch);
并调试结果
答案 2 :(得分:0)
您正在发布一个数组,因此请添加以下行:
curl_setopt ($ch, CURLOPT_HTTPHEADER, Array("Content-Type: multipart/form-data"));