Created
September 16, 2013 18:02
-
-
Save jmertic/6584226 to your computer and use it in GitHub Desktop.
Exporting a report as a PDF via Web Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // specify the REST web service to interact with | |
| $url = '<<your sugar instance URL>>/service/v4_1/rest.php'; | |
| // And admin username/password | |
| $username = '<<username>>'; | |
| $password = '<<password>>'; | |
| // And the report_id you wish to export | |
| $report_id = '<<report id>>'; | |
| // And the filename to create | |
| $pdf_filename = '/path/to/export/report.pdf'; | |
| // Open a curl session for making the call | |
| $curl = curl_init($url); | |
| // Tell curl to use HTTP POST | |
| curl_setopt($curl, CURLOPT_POST, true); | |
| // Tell curl not to return headers, but do return the response | |
| curl_setopt($curl, CURLOPT_HEADER, false); | |
| curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); | |
| // Set the POST arguments to pass to the Sugar server | |
| $parameters = array( | |
| 'user_auth' => array( | |
| 'user_name' => $username, | |
| 'password' => md5($password), | |
| ), | |
| ); | |
| $json = json_encode($parameters); | |
| $postArgs = array( | |
| 'method' => 'login', | |
| 'input_type' => 'JSON', | |
| 'response_type' => 'JSON', | |
| 'rest_data' => $json, | |
| ); | |
| curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); | |
| // Make the REST call, returning the result | |
| $response = curl_exec($curl); | |
| // Convert the result from JSON format to a PHP array | |
| $result = json_decode($response); | |
| if ( !is_object($result) ) { | |
| die("Error handling result.\n"); | |
| } | |
| if ( !isset($result->id) ) { | |
| die("Error: {$result->name} - {$result->description}\n."); | |
| } | |
| // Get the session id | |
| $sessionId = $result->id; | |
| // Export the given report | |
| $parameters = array( | |
| 'session' => $sessionId, | |
| 'report_id' => $report_id, | |
| ); | |
| $json = json_encode($parameters); | |
| $postArgs = array( | |
| 'method' => 'get_report_pdf', | |
| 'input_type' => 'JSON', | |
| 'response_type' => 'JSON', | |
| 'rest_data' => $json, | |
| ); | |
| curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); | |
| // Make the REST call, returning the result | |
| $response = curl_exec($curl); | |
| // Convert the result from JSON format to a PHP array | |
| $result = json_decode($response); | |
| file_put_contents($pdf_filename, base64_decode($result->file_contents)); | |
| echo "PDF saved to $pdf_filename\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment