Steve Price
Steve Price

Reputation: 600

Return a variable after cURL process complete

I have written some script that resides on two different servers and connected by cURL. This is my first attempt at cURL and it's taken a day of reading and some trial and error to get to where I am now. The scripts are working on all points apart from needing to have the $valid variable passed back to ServerA when ServerB has completed so that I can use it to trigger a message stack error system on SiteA.

On serverA I have the following:

<?php
$domain = HTTP_SERVER;
$domain_name = substr($domain, 7);
$module_name = "SagePayServer";

//set POST variables
$url = 'localhost/zc151/validity_check.php';
$fields = array(
                    'dname' => urlencode($domain_name),
                    'mname' => urlencode($module_name)
            );

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
?>

On serverB I have:

<?php
require('includes/application_top.php');
$domain = $_POST['dname'];
$module = $_POST['mname'];

$install_exists_query = "SELECT * FROM al_monitor WHERE module_name = '$module' AND domain_name = '$domain'";
$install_exists_result = $db->Execute($install_exists_query);

if ($install_exists_result->RecordCount() == 0) {
$valid = 0; 
}else{
// Calculate dates
$todays_date = strtotime("now");
$delta_eleven = strtotime("+11 months");
$delta_364 = strtotime("+1 year -1 day");
$delta_year = strtotime("+1 year");

//the values you have above are TIMESTAMPS (the number of seconds since January 1 1970 00:00:00 UTC)
//if you want dates, you can convert as follows
$idate = date("d-m-Y", $todays_date);
$wdate= date("d-m-Y", $delta_eleven);
$fwdate = date("d-m-Y", $delta_364);
$edate = date("d-m-Y", $delta_year);
// -- --------------------------------------------------------

//WRITE TO THE DATABASE
$sql = "UPDATE al_monitor SET idate = '$idate' WHERE
        module_name = '$module' AND domain_name = '$domain'";
$db->Execute($sql);
$sql = "UPDATE al_monitor SET wdate = '$wdate' WHERE
        module_name = '$module' AND domain_name = '$domain'";
$db->Execute($sql);
$sql = "UPDATE al_monitor SET fwdate = '$fwdate' WHERE
        module_name = '$module' AND domain_name = '$domain'";
$db->Execute($sql);
$sql = "UPDATE al_monitor SET edate = '$edate' WHERE
        module_name = '$module' AND domain_name = '$domain'";
$db->Execute($sql);
// -- --------------------------------------------------------   
$valid=1;
}
// End insert module installation date into central database

How can I pass the variable back so that I can have an if/else statement in the script on SiteA?

Upvotes: 0

Views: 105

Answers (1)

scrowler
scrowler

Reputation: 24406

From the manual:

CURLOPT_RETURNTRANSFER: Set value to TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Try:

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

//execute post
$result = curl_exec($ch);

$result should now contain the returned data from your CURL request.


You'll also need to output the $value in your script on Server B after you've finished processing things:

echo $value;

Upvotes: 3

Related Questions