[英]PHP Curl : Get data from XML after submission
I need to get the response after the form is submitted.我需要在提交表单后得到回复。 The form is submitted to a remote API server.
表单被提交到远程 API 服务器。
Here is some of the code:这是一些代码:
/*
* 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;
}
}
I did not write this code and I'm not so familiar with curl.这段代码不是我写的,我对 curl 不太熟悉。 I need to get the
$xml[0]['leadid']
response into another php file.我需要将
$xml[0]['leadid']
响应放入另一个 php 文件中。 Is this possible?这可能吗? Do I include the php file that has these function then create a new function?
我是否包含具有这些功能的 php 文件然后创建一个新功能? Or do I store it on my database then retrieve it from the database?
或者我是否将它存储在我的数据库中然后从数据库中检索它?
Would appreciate any help or further information!将不胜感激任何帮助或进一步的信息!
You can get it from $this->returned['leadid'] if returned class member is public OR create a function如果返回的类成员是公共的,您可以从 $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 响应页面中。
You could just use file_put_contents('http://path.to.another.php',$this->returned['leadid']);
你可以只使用
file_put_contents('http://path.to.another.php',$this->returned['leadid']);
if you have fopen wrappers enabled.如果您启用了 fopen 包装器。 The other php would have to just capture the transferred data.
另一个 php 只需要捕获传输的数据。
Ok so I figured this out on my own after playing around with the classes etc. :好的,所以我在玩完课程等之后自己想通了这一点:
//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];
}
Thanks for your answers and inputs though!感谢您的回答和投入!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.