在cURL过程完成后返回变量

时间:2014-08-08 00:21:39

标签: php curl

我编写了一些驻留在两个不同服务器上并通过cURL连接的脚本。这是我在cURL的第一次尝试,它需要一天的阅读和一些试验和错误才能到达我现在的位置。除了在ServerB完成时需要将$ valid变量传递回ServerA之外,脚本正在处理所有问题,以便我可以使用它来触发SiteA上的消息堆栈错误系统。

在serverA上我有以下内容:

<?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);
?>

在serverB上我有:

<?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

如何将变量传回去,以便我可以在SiteA的脚本中使用if / else语句?

1 个答案:

答案 0 :(得分:3)

From the manual:

  

CURLOPT_RETURNTRANSFER :将值设置为TRUE,将传输作为curl_exec()返回值的字符串返回,而不是直接输出。

尝试:

//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现在应该包含CURL请求中返回的数据。


在您完成处理后,您还需要在服务器B上的脚本中输出$value

echo $value;