Comments (10)
I don't think there is a bug or anything to fix there. Creating an attachment works fine for me and so does passing in the parameter to include with online invoicing:
$XeroOAuth->request
('PUT', $XeroOAuth->url('Invoice/'.$invoices->Invoices[0]->Invoice->InvoiceID.'/Attachments/image.png', 'core'),
array('IncludeOnline' => 'True'),
$attachmentFile,
'file');
Let me know if I am missing something, but I don't think anything needs to be changed with the existing code.
from xerooauth-php.
Hello,
I just had the same problem. I found a bug in lib/XeroOAuth.php where it will always set the mime type to be application/x-www-form-urlencoded instead of application/pdf so Xero rejects it. To fix it find:
if ($this->format == "file") {
$put_body = $this->xml;
} else {
$put_body = $this->safe_encode ( $this->xml );
$this->headers ['Content-Type'] = 'application/x-www-form-urlencoded';
}
And replace it with:
switch ($this->format) {
case "file" :
$put_body = $this->xml;
break;
case "pdf" :
$put_body = $this->xml;
$this->headers ['Content-Type'] = 'application/pdf';
break;
case "json" :
$put_body = $this->xml;
$this->headers ['Content-Type'] = 'application/json';
break;
default :
$put_body = $this->safe_encode ( $this->xml );
$this->headers ['Content-Type'] = 'application/x-www-form-urlencoded';
break;
}
Then make sure you set pdf as the format when you call the put request:
$response = $XeroOAuth->request('PUT', $XeroOAuth->url('Invoice/'.$invoices->Invoices[0]->Invoice->InvoiceID.'/Attachments/image.png', 'core'), array(), $attachmentFile, 'pdf');
from xerooauth-php.
Hello,
Thanks for replying , and thanks for the great method, not sure the Parameters $attachmentFile need tp be raw data in string or in some kind of xml structure
from xerooauth-php.
That was just from the tests/tests.php file so the full section is:
if ($_REQUEST['invoice']=="attachment") {
$attachmentFile = file_get_contents('http://i.imgur.com/mkDFLf2.png');
$response = $XeroOAuth->request('PUT', $XeroOAuth->url('Invoice/'.$invoices->Invoices[0]->Invoice->InvoiceID.'/Attachments/image.png', 'core'), array(), $attachmentFile, 'file');
if ($XeroOAuth->response['code'] == 200) {
echo "Attachment successfully created against this invoice.";
} else {
outputError($XeroOAuth);
}
}
So $attachmentFile is just the raw data of PDF
I am using it by just doing a put request with the contents of the pdf, then reading that into the variable:
$attachmentFile = file_get_contents('php://input');
from xerooauth-php.
Thanks a lot, that works!
from xerooauth-php.
Hi
I found another problem, when i try to include the attachment online, by using this method it get a 500 respond,
so are there any way to change the post method as well?
i did this: but it seem not working
case 'POST' :
$fh = tmpfile();
switch ($this->format) {
case "xml":
curl_setopt ( $c, CURLOPT_POST, TRUE );
$post_body = $this->safe_encode ( $this->xml );
curl_setopt ( $c, CURLOPT_POSTFIELDS, $post_body );
$this->request_params ['xml'] = $post_body;
$contentLength = strlen ( $post_body );
$this->headers ['Content-Type'] = 'application/x-www-form-urlencoded';
break;
case "pdf":
$put_body = $this->xml;
$this->headers ['Content-Type'] = 'application/pdf';
fwrite ( $fh, $put_body );
rewind ( $fh );
curl_setopt ( $c, CURLOPT_POST, true );
curl_setopt ( $c, CURLOPT_POSTFIELDS, $fh );
curl_setopt ( $c, CURLOPT_INFILESIZE, strlen ( $put_body ) );
$contentLength = strlen ( $put_body );
break;
}
break;
reference for the include attachment online : http://developer.xero.com/documentation/api/attachments/
search key word : Include with Online Invoice
🙇
from xerooauth-php.
I don't think it is going make a difference with that for put/post. From the document:
"The PUT method is identical to the POST method."
When I add ?IncludeOnline=true to the URL I get:
oauth_problem=consumer_key_unknown&oauth_problem_advice=Consumer%20key%20was%20not%20recognised
So I am guessing there is a bug somewhere with parsing the URL, then causing a problem with oAuth.
from xerooauth-php.
I've found the first bug which is that it is adding a second ? to the URL.
I fixed that by altering the sign function in OAuthSimple.php to be
$this->_parameters['oauth_signature'] = $this->_generateSignature($normParams);
if (strpos($this->_path,'?') !== false) {
$returnURL = $this->_path . '&' . $this->_normalizedParameters('true');
} else {
$returnURL = $this->_path . '?' . $this->_normalizedParameters('true');
}
return Array(
'parameters' => $this->_parameters,
'signature' => $this->_oauthEscape($this->_parameters['oauth_signature']),
'signed_url' => $returnURL,
'header' => $this->getHeaderString(),
'sbs'=> $this->sbs
);
}
So that will now look for a ? in the url and if it is there use an & instead of a ? so the oauth parameters get passed as well.
However I am now getting:
oauth_problem=signature_invalid&oauth_problem_advice=Failed%20to%20validate%20signature
So somewhere else the ? must be messing up the signing!
I don't understand why Xero didn't just set this as another variable you can pass in with the XML as it would make it a million times easier!
I'm still getting my head around how oAuth works so not sure if I will be able to find why the signing is failing now.
from xerooauth-php.
I have done some test, it seems no matter where we add IncludeOnline=true it will cause the oauth problem, it seems there is nothing we can do at this stage.
Thanks for the bug fix
from xerooauth-php.
Hi Ronanq
Thanks for reply, that works for me as well.
Just come with another question
Are there any way to sent the invoice through API?
from xerooauth-php.
Related Issues (20)
- Curl Error
- contactpersons HOT 3
- SHA-2? HOT 2
- TokenExpired:
- composer HOT 1
- Add option to disable ipv6 HOT 6
- Does API provide this functionality?
- PHP Version Support HOT 2
- Need help to fix Unknown SSL protocol error in connection during refresh tokens HOT 4
- Invoice Payment dates are returned in .NET DataContractJsonSerializer format
- What is the point of $this->config ['session_handle']
- Failure with blank 'curl_error' executing request. HOT 6
- Uncompleted composer support
- Unresolved conflict commit!
- Entrust Certificate not properly deprecated? HOT 2
- where to insert the 7 digit code after authorization
- Undefined index: session_handle HOT 7
- how to get contact persons using contact id or contact name? HOT 1
- How to use the API to pull the Bank Statement?
- Accept JSON post data HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xerooauth-php.