2016-05-12 36 views
0

我正在收集表單上的數據並通過POST將數據發送到收集和分發表單主管的外部服務器。外部服務器將XML響應發送回我的服務器。如何在收到POST響應時運行php?

我已經爲響應php腳本設置了重定向URL。我希望腳本解析XML響應並激發其中一個字段中提供的像素。我也希望系統在每次收到回覆時給我發電子郵件。

How the system is layed out

我的問題

該系統的工作,如果我手動輸入XML響應,並在我的 瀏覽器加載它。當在 背景中發送迴應時,它不執行腳本。

使用tshark我一直在監視流量,並且每次提交表單時,php腳本都會被命中,但沒有任何執行。

我當前的代碼

<?php 

//Example Post from Lead Server 
$server_output2= 
'<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <status>Unmatched</status> 
    <lead_id>69</lead_id> 
    <tracking_pixel>https://demo.leadportal.com/genericPostlead.php?TYPE=79&amp;amp;SRC=pixeltestingforcege</tracking_pixel> 
</response>'; 

//Live Server Response 
$server_output = file_get_contents('php://input'); 
if (!empty($server_output)) { 
    $xml = simplexml_load_string($server_output) or die("Error: Cannot create object"); 
} 

//XML Parse of Lead Server Response 
$status=$xml->status; 
$lead_id=$xml->lead_id; 
$pixel=$xml->tracking_pixel; 

?> 

<!-- This is the HTML Section to test and Display the Status and LeadID --> 
The status is <span style="font-weight:bold;color:red;"><?php if(!empty($status)) echo $status; else{echo "n/a";} ?></span> and the lead id is <span style="font-weight:bold;color:red;"><?php if(!empty($lead_id)) echo $lead_id; else{echo "n/a";} ?></span> 

<br> 
<!-- This is the Pixel Fire from the Response--> 
<h3>Pixel Here! 
<iframe src="<?php echo $pixel; ?>" height="1" width="1" frameborder="1" style="border:medium double red"></iframe></h3> 

<?php 
$to = "[email protected]"; 
$subject = "XML Response"; 
$message =" 
Hello 
$xml 
$server_output 
$server_output2 

"; 

$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 


mail($to,$subject,$message,$headers); 
?> 
+0

一個重定向總是來自瀏覽器的'GET'請求時,它不能從外部服務器發送'POST'數據。 – Barmar

回答

0

問題

我最後不得不在這個項目中的大量限制,由於第三方服務器以及我們的使用WordPress的忍者與形式。

基本上,第三方服務無法將其響應發送到特定位置。它只是返回了原始數據發佈的發佈回覆。

除非在調試模式下,否則忍者窗體沒有辦法捕獲響應。當在調試模式下它創建了一個陣列,其必須被解析

我創建一個插件來打入忍窗體網絡掛接插件和拉出從那裏的響應。

然後我分析了對日誌的XML響應,並獲取了必要的值,包括像素URL。

Finallly我用捲曲火像素

守則

<?php 
public function fire_pixel ($response) { 


     $filename = './webhook.log'; 
     $xml_array = $response['body']; 


     if (!empty($xml_array)) { 
      $xml = simplexml_load_string($xml_array) or die("Error: Cannot create object"); 

       $xml_status = $xml->status; 
       $lead_id = $xml->lead_id; 
       $pixel = $xml->tracking_pixel; 
       $xml_error = $xml->error; 

        if ($xml_error == ''){ 
         $xml_error = "No Error"; 
        } 
        if ($lead_id == ''){ 
         $lead_id = "Error"; 
        } 
        if ($pixel == ''){ 
         $pixel_message = "No Tracking Pixel"; 
        } 
        else { 
         $pixel_message = ''; 
        } 

      $log = "Date: ".date("F j, Y, g:i a").PHP_EOL. 
        "Status: ".$xml_status.PHP_EOL. 
        "Lead ID: ".$lead_id.PHP_EOL. 
        "Pixel: ".$pixel.$pixel_message.PHP_EOL. 
        "Error: ".$xml_error.PHP_EOL. 
        "-------------------------".PHP_EOL; 
      file_put_contents($filename, $log, FILE_APPEND); 

      if (!empty($pixel)) { 
       $pixel_results = file_get_contents($pixel); 

     $date = date(DATE_RFC2822); 
     $current = file_get_contents($filename); 
     $current .= $date; 
     $current .= $pixel_results; 
     $current .= " : cURL : cURL STARTED | cURL started\n"; 
     file_put_contents($filename, $current); 


      $ch = curl_init(); 

       curl_setopt($ch, CURLOPT_URL, $pixel); 
       curl_exec($ch); 

      $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 


     $current = file_get_contents($filename); 
     $current .= $date . " : cURL : cURL STATUS | " . $http_code . "\n"; 
     file_put_contents($filename, $current); 

     curl_close($ch); 
     } 
    } 
} 
    ?> 
相關問題