[英]PHP Curl : Get data from XML after submission
我需要在提交表單后得到回復。 表單被提交到遠程 API 服務器。
這是一些代碼:
/*
* Submits the data via a CURL session
*/
private function sendDetails(){
if(true || $_SERVER['REMOTE_ADDR'] != '85.17.27.88'){
$ch = curl_init($this->parseLink);
curl_setopt_array($ch, array(
CURLOPT_FRESH_CONNECT => true,
CURLOPT_HEADER => false,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_ENCODING => "",
CURLOPT_POSTFIELDS => $this->parseRequestString($this->result)
));
$this->returned = curl_exec($ch);
$this->headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$this->result = $this->checkResponse();
if(!$this->result)
$this->setError($this->returned);
elseif(isset($this->p['mobi-submit'])) {
//redirect mobi users
$_SESSION['enquire-success-name'] = $this->p['enquire-name'];
wp_redirect('');
exit;
}
} else {
echo nl2br(var_export($this->result, true));
exit;
}
}
/*
* Checks the response from the webservice for errors / success
*/
private function checkResponse(){
libxml_use_internal_errors(true);
try {
$xml = new SimpleXMLElement($this->returned);
} catch(Exception $e){
return false;
}
if($xml) {
// If the response has a leadid attrib, then we submitted it successfully.
if(!empty($xml[0]['leadid'])) {
if(is_string($xml[0]))
$this->returned = $xml[0];
return true;
}
// If the errorcode is 7, then we have a resubmit and so it was successful.
elseif(!empty($xml->errors->error[0]['code']) && $xml->errors->error[0]['code'] == "7") {
if(is_string($xml->errors->error[0]))
$this->returned = $xml->errors->error[0];
return true;
}
// Otherwise try set the response to be the errormessage
elseif(!empty($xml->errors->error[0])){
if(is_string($xml->errors->error[0]))
$this->returned = $xml->errors->error[0];
return false;
}
// Otherwise set it to the first xml element and return false.
else {
if(is_string($xml[0]))
$this->returned = $xml[0];
return false;
}
}
// If the xml failed, revert to a more rudimentary test
elseif(stripos($this->returned, $this->expected) !== false) {
return true;
}
// If that also fails, expect error.
else {
return false;
}
}
這段代碼不是我寫的,我對 curl 不太熟悉。 我需要將$xml[0]['leadid']
響應放入另一個 php 文件中。 這可能嗎? 我是否包含具有這些功能的 php 文件然后創建一個新功能? 或者我是否將它存儲在我的數據庫中然后從數據庫中檢索它?
將不勝感激任何幫助或進一步的信息!
如果返回的類成員是公共的,您可以從 $this->returned['leadid'] 獲取它,或者創建一個函數
public function getReturnedVal($key = 'leadid'){
if(isset($this->returned[$key])){
return $this->returned[$key];
}
return "";
}
正如您在問題中所說的那樣,最簡單的選擇而不是處理會話或數據庫來存儲 API 響應,而是將您的腳本包含在需要數據的 PHP 文件中( include('curl_api_script_file.php');
)和然后使用類似@volkinc 建議的函數將數據回顯到您需要的 PHP 響應頁面中。
你可以只使用file_put_contents('http://path.to.another.php',$this->returned['leadid']);
如果您啟用了 fopen 包裝器。 另一個 php 只需要捕獲傳輸的數據。
好的,所以我在玩完課程等之后自己想通了這一點:
//Get the type of lead that is submitted
$lead_type = $insureFormResult->p['enquire-type'];
//Get the leadid from xml
$xml = $insureFormResult->returned;
if($xml){
$xml1 = new SimpleXMLElement($xml);
$leadid = $xml1->xpath("/response/@leadid")[0];
}
感謝您的回答和投入!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.