problem sending varibles to remote url with CURL
I'm trying to send variables from server A to server B, and back. I have everything working other then actually sending the variables from server A to server B. So i can send variables back from server B to server A but just cant send them to Server B from server A. I use JSON to send the variables back (which works fine) and i use _POST
to send them to server B.
Here is my code on both Servers:
Server 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;
?>
Server 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["F开发者_如何学JAVAirst_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);
?>
I know that everything else works fine as I've replaced:
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = '$username' AND Unique_id = '$reference'");
WITH
$select = mysql_query("SELECT * FROM $usertable WHERE ". "GDI_Username = 'myusername' AND Unique_id = '45415645154'");
So i know the problem lies within sending the variables (
'unq__id' => $sponsor_reference,
'gdi__username' => $sponsor_GDI_id,
from server A as i cant use them in the script on server B)
When i test it using the variables, I just get a blank page, but when i replace that line as mentioned above, i get the name ($sponsor_first_nme) echoed out (the expected result)
You could use serialize() to create a string representation of whatever you are trying to send.
You may have to set the post option before you set the post data.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
If not, the post data could be empty.
Send the post data as a string
$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,'&');
Now you can send it in curl:
Edit:
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
I've got it working, well it seemed to be working the whole time...
I was just echoing it out incorrectly. I used echo $sponsor_first_nme
when it was supposed to be echo "$sponsor_first_nme"
(with quotes).
Really appreciate all your help though! Thanks a BUNCH!
精彩评论