我正在尝试将变量从服务器A发送到服务器B,然后返回。我有其他工作,然后实际上从服务器A发送变量到服务器B.所以我可以将变量从服务器B发送回服务器A,但只是无法从服务器A发送到服务器B.我使用JSON发回变量(哪个工作正常)我使用_POST
将它们发送到服务器B.
这是我在两台服务器上的代码:
服务器A
<?
require ('../refference.php');
$post_fields = array(
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
);
$ch = curl_init('http://site.com/WP/d__access.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
$data = json_decode($result);
$sponsor_first_nme = $data->sponsor_first_nme;
echo $sponsor_first_nme;
?>
服务器B
<?
include ('config/wp__2135432135435135412312415456654452547534.php');
mysql_connect($hostname,$username,$password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$reference = $_POST['unq__id'];
$username = $_POST['gdi__username'];
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = '$username' AND Unique_id = '$reference'");
while($check = mysql_fetch_array($select)) {
$sponsor_email = $check["Email"];
$sponsor = $check["GDI_Username"];
$sponsor_first_nme = $check["First_Name"];
$sponsor_second_nme = $check["Last_Name"];
$sponsor_domain = $check["GDI_Domain"];
$unq_id = $check["Unique_id"];
}
$sponsor_name = "$sponsor_first_nme $sponsor_second_nme";
$result = array(
'sponsor_first_nme' => $sponsor_first_nme,
'sponsor_second_nme' => $sponsor_second_nme,
'sponsor_email' => $sponsor_email,
'sponsor' => $sponsor,
'sponsor_domain' => $sponsor_domain,
'unq_i' => $unq_id,
'sponsor_full_name' => $sponsor_name,
);
echo json_encode($result);
?>
我知道其他一切正常,因为我已经取代:
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = '$username' AND Unique_id = '$reference'");
使用
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = 'myusername' AND Unique_id = '45415645154'");
所以我知道问题在于发送变量(
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
来自服务器A,因为我无法在服务器B上的脚本中使用它们)
当我使用变量测试它时,我只得到一个空白页面,但当我如上所述替换该行时,我得到了名称($ sponsor_first_nme)回显(预期结果)
答案 0 :(得分:0)
将发布数据作为字符串发送
$fields = array(
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
);
$field_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
现在你可以用curl发送它:
修改强>
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
答案 1 :(得分:0)
您可以使用serialize()创建您要发送的任何内容的字符串表示。
答案 2 :(得分:0)
您可能必须在设置发布数据之前设置发布选项。
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
如果没有,帖子数据可能是空的。
答案 3 :(得分:0)
我已经开始工作了,好像它似乎一直在工作......
我只是错误地回应了它。我使用echo $sponsor_first_nme
时应该echo "$sponsor_first_nme"
(带引号)。
真的很感谢你的帮助!谢谢BUNCH!