【问题标题】:PHP Curl : Get data from XML after submissionPHP Curl:提交后从 XML 获取数据
【发布时间】: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


    【解决方案1】:

    如果返回的类成员是公共的,你可以从 $this->returned['leadid'] 获取它或者创建一个函数

    public function getReturnedVal($key = 'leadid'){
      if(isset($this->returned[$key])){
        return $this->returned[$key];
      }
      return "";
    }
    

    【讨论】:

    • 感谢您的回答,但我如何在另一个 php 文件中回显呢?
    • 简单的方法是保存在 $_SESSION 中,如果这是同一主机或存储在数据库中
    • 简单的方法是保存在 $_SESSION 中,如果这是相同的主机或存储在数据库中,如果不同的主机和应用程序头(“位置:主机/文件名.php?id =”。 $idvalue);
    • 如何将它存储在会话中?
    • session_start(); $_SESSION['leadid'] = $this->returned['leadid'];
    【解决方案2】:

    正如您在问题中所说,与处理会话或数据库来存储 API 响应相比,您最简单的选择是将您的脚本包含在您需要数据的 PHP 文件中 (include('curl_api_script_file.php');),然后使用@volkinc 之类的函数建议将数据回显到您需要的 PHP 响应页面中。

    【讨论】:

      【解决方案3】:

      如果您启用了 fopen 包装器,您可以只使用 file_put_contents('http://path.to.another.php',$this->returned['leadid']);。 其他 php 必须只捕获传输的数据。

      【讨论】:

        【解决方案4】:

        好的,所以我在玩过课程等之后自己想出了这个:

        //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];
        }
        

        感谢您的回答和投入!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-08
          • 1970-01-01
          • 2012-07-18
          • 1970-01-01
          • 2017-07-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多