2015-05-08 73 views
2

我想要發票XERO。下面是函數使用API​​將發票過帳到XERO

<?php 
Function CallAPI($url, $method, $parameters, $options, &$response) 
    { 
    if (!IsSet($options['Resource'])) $options['Resource'] = 'API call'; 
    if (!IsSet($options['ConvertObjects'])) $options['ConvertObjects'] = false; 
    $version = intval($this->oauth_version); 
    $two_legged = ($version === 1 && IsSet($options['2Legged']) && $options['2Legged']); 
    if (strlen($this->access_token) === 0 && !$two_legged) 
     { 
     if (!$this->RetrieveToken($valid)) return false; 
     if (!$valid) return $this->SetError('the access token is not set to a valid value'); 
     } 

    switch ($version) 
     { 
    case 1: 
     if (!$two_legged && strlen($this->access_token_expiry) && strcmp($this->access_token_expiry, gmstrftime('%Y-%m-%d %H:%M:%S')) <= 0) 
      { 
      if (strlen($this->refresh_token) === 0) return ($this->SetError('the access token expired and no refresh token is available')); 
      if ($this->debug) $this->OutputDebug('Refreshing the OAuth access token expired on ' . $this->access_token_expiry); 
      $oauth = array(
       'oauth_token' => $this->access_token, 
       'oauth_session_handle' => $this->refresh_token 
      ); 
      if (!$this->ProcessToken1($oauth, $access_token)) return false; 
      if (IsSet($options['FailOnAccessError']) && $options['FailOnAccessError'] && strlen($this->authorization_error)) 
       { 
       $this->error = $this->authorization_error; 
       return false; 
       } 

      if (!IsSet($access_token['authorized']) || !$access_token['authorized']) return ($this->SetError('failed to obtain a renewed the expired access token')); 
      $this->access_token = $access_token['value']; 
      $this->access_token_secret = $access_token['secret']; 
      if (IsSet($access_token['refresh'])) $this->refresh_token = $access_token['refresh']; 
      } 

     $oauth = array(); 
     if (!$two_legged) $oauth[strlen($this->access_token_parameter) ? $this->access_token_parameter : 'oauth_token'] = $this->access_token; 
     break; 

    case 2: 
     if (strlen($this->access_token_expiry) && strcmp($this->access_token_expiry, gmstrftime('%Y-%m-%d %H:%M:%S')) <= 0) 
      { 
      if (strlen($this->refresh_token) === 0) return ($this->SetError('the access token expired and no refresh token is available')); 
      if ($this->debug) $this->OutputDebug('Refreshing the OAuth access token expired on ' . $this->access_token_expiry); 
      if (!$this->ProcessToken2(null, true)) return false; 
      if (IsSet($options['FailOnAccessError']) && $options['FailOnAccessError'] && strlen($this->authorization_error)) 
       { 
       $this->error = $this->authorization_error; 
       return false; 
       } 
      } 

     $oauth = null; 
     if (strcasecmp($this->access_token_type, 'Bearer')) $url.= (strcspn($url, '?') < strlen($url) ? '&' : '?') . (strlen($this->access_token_parameter) ? $this->access_token_parameter : 'access_token') . '=' . UrlEncode($this->access_token); 
     break; 

    default: 
     return ($this->SetError($this->oauth_version . ' is not a supported version of the OAuth protocol')); 
     } 

    return ($this->SendAPIRequest($url, $method, $parameters, $oauth, $options, $response)); 
    } 

,我不斷收到此錯誤

我成功連接到Xero的API, 但我目前陷進去POST使用XML的發票。

<?php 
$url = 'xml/invoice.xml'; 
$xml = simplexml_load_file($url); 
$success3 = $client->CallAPI('https://api.xero.com/api.xro/2.0/Invoices', 'POST', array(
    'format' => 'text/xml', 
    'File' => $xml 
) , array(
    'FailOnAccessError' => true, 
    'DecodeXMLResponse' => 'simplexml' 
) , $user3); 

這是我有的代碼,但我不知道這是如何工作的。 這是我作爲迴應得到的錯誤。

SimpleXMLElement Object 
(
[ErrorNumber] => 17 
[Type] => NoDataProcessedException 
[Message] => No data has been processed for this endpoint. This endpoint is expecting Invoice data to be specifed in the request body. 
) 

這裏是我使用

<?php 
/* 
* login_with_xero.php 
* 
* @(#) $Id: login_with_xero.php,v 1.2 2014/03/16 12:41:48 mlemos Exp $ 
* 
*/ 
/* 
* Get the http.php file from http://www.phpclasses.org/httpclient 
*/ 
require ('http.php'); 

require ('oauth_client.php'); 

$client = new oauth_client_class; 
$client->debug = false; 
$client->debug_http = true; 
$client->server = 'Xero'; 
$client->redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . dirname(strtok($_SERVER['REQUEST_URI'], '?')) . '/login_with_xero.php'; 
$client->client_id = '3TBW9EI6KFFD6GDEKEDSTKZXHFVOLK'; 
$application_line = __LINE__; 
$client->client_secret = 'OQZJTYDZQH0CTEKNH1MWCTY3AJFTU0'; 

if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0) die('Please go to Xero Applications page https://api.xero.com/Application/Add , ' . 'create an application, and in the line ' . $application_line . ' set the client_id to Consumer key and client_secret with Consumer secret. ' . 'The Callback URL must be ' . $client->redirect_uri . ' If you want to post to ' . 'the user timeline, make sure the application you create has write permissions'); 

if (($success = $client->Initialize())) 
    { 
    if (($success = $client->Process())) 
     { 
     if (strlen($client->access_token)) 
      { 
      $success = $client->CallAPI('https://api.xero.com/api.xro/2.0/Users', 'GET', array() , array(
       'FailOnAccessError' => true, 
       'DecodeXMLResponse' => 'simplexml' 
      ) , $user); 
      $success2 = $client->CallAPI('https://api.xero.com/api.xro/2.0/Invoices', 'GET', array() , array(
       'FailOnAccessError' => true, 
       'DecodeXMLResponse' => 'simplexml' 
      ) , $user2); 
      $url = 'xml/invoice.xml'; 
      $xml = simplexml_load_file($url); 
      echo $xml; 
      $success3 = $client->CallAPI('https://api.xero.com/api.xro/2.0/Invoices', 'POST', array(
       'format' => 'text/xml', 
       'File' => $xml 
      ) , array(
       'FailOnAccessError' => true, 
       'DecodeXMLResponse' => 'simplexml' 
      ) , $user3); 
      } 
     } 

    $success = $client->Finalize($success); 
    } 

if ($client->exit) exit; 

if ($success) 
    { 
?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
    <head> 
    <title>Xero OAuth client results</title> 
    </head> 
    <body> 
    <?php 
    echo '<h1>', HtmlSpecialChars($user->Users->User->FirstName) , ' you have logged in successfully with Xero!</h1>'; 
    echo '<pre>', HtmlSpecialChars(print_r($user, 1)) , '</pre>'; 
    echo '<pre>', HtmlSpecialChars(print_r($user2, 1)) , '</pre>'; 
    echo '<pre>', HtmlSpecialChars(print_r($user3, 1)) , '</pre>'; 
?> 
    </body> 
    </html> 
    <?php 
    } 
    else 
    { 
?> 
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
    <html> 
    <head> 
    <title>OAuth client error</title> 
    </head> 
    <body> 
    <h1>OAuth client error</h1> 
    <pre>Error: <?php 
    echo HtmlSpecialChars($client->error); ?></pre> 
    </body> 
    </html> 
    <?php 
    } 

?> 

回答

0

變化'format' => 'text/xml'format' => 'xml'代碼。