2013-04-12 62 views
1

我有一個網絡應用程序,我需要與快速書在線版進行交流。我可以連接和接收數據。問題是,我得到的迴應不是xml格式,或者更好的是在json合成器中。我怎樣才能讓它以這些格式之一作出迴應?這裏是我的代碼:快速書在線交互

function request($xml, $certificate = null, $debug = false){ 
    $ch = curl_init(); 

    $header[] = 'Content-Type: application/x-qbxml'; 
    $header[] = 'Content-Length: ' . strlen($xml); 

    $params = array(); 
    $params[CURLOPT_HTTPHEADER] = $header; 
    $params[CURLOPT_POST] = true; 
    $params[CURLOPT_RETURNTRANSFER] = true; 
    $params[CURLOPT_URL] = $this->gateway; 
    $params[CURLOPT_TIMEOUT] = 30; 
    $params[CURLOPT_POSTFIELDS] = $xml; 
    $params[CURLOPT_VERBOSE] = $debug; 
    $params[CURLOPT_HEADER] = true; 

    // This is only for the HOSTED security model 
    if (file_exists($certificate)) 
    { 
     $params[CURLOPT_SSL_VERIFYPEER] = false; 
     $params[CURLOPT_SSLCERT] = $certificate; 
    } 

    // Some Windows servers will fail with SSL errors unless we turn this off 
    $params[CURLOPT_SSL_VERIFYPEER] = false; 
    $params[CURLOPT_SSL_VERIFYHOST] = 0;   

    // Diagnostic information: https://merchantaccount.quickbooks.com/j/diag/http 
    // curl_setopt($ch, CURLOPT_INTERFACE, '<myipaddress>'); 

    $ch = curl_init(); 
    curl_setopt_array($ch, $params); 
    $response = curl_exec($ch); 

    if (curl_errno($ch)) 
    { 
     $errnum = curl_errno($ch); 
     $errmsg = curl_error($ch); 

     _log('CURL error: ' . $errnum . ': ' . $errmsg, $debug); 

     return false; 
    } 

    // Close the connection 
    @curl_close($ch); 

    // Remove the HTTP headers from the response 
    $pos = strpos($response, "\r\n\r\n"); 
    $response = ltrim(substr($response, $pos)); 

    return $response;  
} 
function item_list(){ 

    $xml = 
     '<?xml version="1.0"?> 
     <?qbxml version="6.0"?> 
     <QBXML> 
      <SignonMsgsRq> 
       <SignonTicketRq> 
        <ClientDateTime>' . date('Y-m-d') . 'T' . date('H:i:s') . '</ClientDateTime> 
        <SessionTicket>' . $this->session . '</SessionTicket> 
        <Language>English</Language> 
        <AppID>' . $this->application_id . '</AppID> 
        <AppVer>1</AppVer> 
       </SignonTicketRq> 
      </SignonMsgsRq> 
      <QBXMLMsgsRq onError="stopOnError"> 
       <ItemQueryRq> 
        <OwnerID>0</OwnerID> 
       </ItemQueryRq> 

      </QBXMLMsgsRq> 
     </QBXML>'; 
     $response = $this->request($xml, null, true); 
     echo $response; 

} 

感謝

這裏我的XML請求: $ XML = ' '。日期('Y-m-d')。 'T'。日期('H:i:s')。 ' '。 $ this-> session。 ' 英文 '。 $ this-> application_id。 '

</QBXMLMsgsRq> 
</QBXML>'; 

這是我的性反應:

2013-04-12T12:28:11 V1-115-Q03kydq32h22tnfqnd5izf:689712285 1 2013-04-10T06:02 :40 2013-04-10T06:02:40 0銷售銷售0 0 1銷售2 2013-04-10T21:20:43 2013-04-10T21:20:43 0測試產品測試產品0 0 1銷售

+0

*回覆您的回覆?它*應該*爲XML格式。發佈您的XML請求和XML響應。 –

+0

這是我回復的回覆:2013-04-12T12:28:11 V1-115-Q03kydq32h22tnfqnd5izf:689712285 1 2013-04-10T06:02:40 2013-04-10T06:02:40 0銷售銷售0 0 1銷售2 2013-04-10T21:20:43 2013-04-10T21:20:43 0測試產品測試產品0 0 1銷售 – rstewart

+0

在上面的帖子中,我嘗試粘貼xml,但看起來像是搞砸了。但函數item_list()顯示了我用於請求的xml。 – rstewart

回答

2

我愚蠢的錯誤。我回應了我的結果,瀏覽器將xml標籤解釋爲html標籤。我用$ xml = simplexml_load_string($ response);的print_r($ XML);在瀏覽器中查看響應。