PHP Curl:在提交后从XML中获取数据


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文件然后创建新函数?还是将其存储在数据库中,然后从数据库中检索?

感谢任何帮助或进一步的信息!

如果返回的类成员是public或者创建了一个函数,可以从$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响应页面,你需要它。

如果启用了fopen包装器,则可以使用file_put_contents('http://path.to.another.php',$this->returned['leadid']);。另一个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];
}

谢谢你的回答和输入!