Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added PDF support. #2

Merged
merged 2 commits into from

2 participants

@robbrit

FreshBooks added the invoice.getPDF command, which returns binary PDF data instead of XML. I changed the library to handle the case when PDF data is sent from FreshBooks and will handle it accordingly.

@jboesch

Thanks for doing this. Can you please adjust the code to follow the same standard as the file. Example 4 spaces for indentation, curly braces on new line for if/else.

lib/FreshBooksRequest.php
@@ -145,7 +145,7 @@ public function getGeneratedXML()
/*
* Send the request over the wire
*
- * @return array
+ * @return binary data if response is a PDF, array if not
@jboesch Owner
jboesch added a note
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@robbrit

Alright, good to go. Let me know if there's any other tweaks that should be done.

@jboesch jboesch merged commit b8d58a9 into from
@jboesch
Owner

Looks great! Thank you for doing this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 28, 2012
  1. Added PDF support.

    Rob authored
  2. Updated for style.

    Rob authored
This page is out of date. Refresh to see the latest.
Showing with 17 additions and 6 deletions.
  1. +17 −6 lib/FreshBooksRequest.php
View
23 lib/FreshBooksRequest.php
@@ -1,4 +1,4 @@
-<?
+<?php
require_once('XmlDomConstruct.php');
/**
* A simple PHP API wrapper for the FreshBooks API.
@@ -143,9 +143,10 @@ public function getGeneratedXML()
}
/*
- * Send the request over the wire
+ * Send the request over the wire. Return result will be binary data if the FreshBooks response is
+ * a PDF, array if it is a normal request.
*
- * @return array
+ * @return mixed
*/
public function request()
{
@@ -177,10 +178,20 @@ public function request()
curl_close($ch);
}
- $response = json_decode(json_encode(simplexml_load_string($result)), true);
+ // With the new invoice.getPDF request, we sometimes have non-XML come through here
+ if (substr($result, 0, 4) == "%PDF")
+ {
+ // it's a PDF file
+ $response = $result;
+ $this->_success = true;
+ }
+ else
+ {
+ $response = json_decode(json_encode(simplexml_load_string($result)), true);
+ $this->_success = ($response['@attributes']['status'] == 'ok');
+ }
$this->_response = $response;
- $this->_success = ($response['@attributes']['status'] == 'ok');
if(isset($response['error']))
{
$this->_error = $response['error'];
@@ -188,4 +199,4 @@ public function request()
}
-}
+}
Something went wrong with that request. Please try again.