如何捕获XML响应数据并将其抛给变量

时间:2015-03-20 15:46:03

标签: php xml curl

以下是我正在使用的代码。我理解95%的。

  1. 我正在使用cURL设置标题并发帖(httpRequest功能)。
  2. 然后我有一个函数(requestTemplate),它对返回XML响应的URL进行POST。
  3. 下一个函数(prefillTemplate)的目的是从XML响应中收集某个变量(for exp $xml->title;)并将其发布到另一个URL。
  4. 我遇到的问题是,如何从XML响应中收集该变量并将其抛给prefillTemplate函数?

    function httpRequest($url, $headers, $method, $body = NULL) {
    if (!$method) {
      $method = "GET";
    };
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // Append 'api-token' to Headers
    curl_setopt($curl, CURLOPT_HTTPHEADER, array($headers, "api-token: $this-  >secure_token")); // Set the headers.
    
    if ($body) {
      curl_setopt($curl, CURLOPT_POST, 1);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
            // Append 'api-token' to Headers
      curl_setopt($curl, CURLOPT_HTTPHEADER, array($headers, "Content-Type: text/xml;charset=utf-8", "api-token: $this->secure_token"));   
    }
    
    $data = curl_exec($curl);
    if ($this->debug) {
      echo "In httpRequest: Recieved DATA\n===========\n" . $data .  "\n===========\n";
    }
    curl_close($curl);
    return $data; 
    }
    
    
    public function requestTemplate(){
    
    
    $url = $this->secure_base_url . "blahblah/prepackage.xml";
    //echo "Getting documents...\n";
    $header = Array();
    $response = $this->httpRequest($url, $header, "POST");
    return $response;
    
    
    }
    
    function prefillTemplate($data){
        $url = $this->secure_base_url . "blahblah/templates.xml";
        $xml = "<?xml version='1.0' encoding='UTF-8'?><template><guid>".$data."</guid></template>";
        echo $xml;
        $header = Array();
        $response = $this->httpRequest($url, $header, "POST", $xml);
        return $response;
      }
    

0 个答案:

没有答案