【发布时间】:2014-07-11 23:14:39
【问题描述】:
我需要在表单提交后得到响应。表单被提交到远程 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 文件然后创建一个新功能?还是我将它存储在我的数据库中,然后从数据库中检索它?
不胜感激任何帮助或更多信息!
【问题讨论】:
标签: php mysql xml wordpress curl