douqiang6448 2014-08-08 00:21
浏览 254
已采纳

在cURL过程完成后返回变量

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?

  • 写回答

1条回答 默认 最新

  • doufei8250 2014-08-08 00:25
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)