/bulkclient/samples/csv.php

http://forceworkbench.googlecode.com/ · PHP · 75 lines · 33 code · 18 blank · 24 comment · 9 complexity · cbf7d1999cff022006c986f7d3717235 MD5 · raw file

  1. <?php
  2. // STEP 1: OBTAIN SESSION ID AND ENDPOINT FROM PARTNER API. REPLACE WITH YOUR ENDPOINT AND SESSION ID.
  3. // For demo purposes, these can just be GET parameters on this page, but should be
  4. // obtained from the login() call using the Force.com Partner API with a username and password.
  5. // In PHP, it is recommended to use the PHP Toolkit to call the Partner API. For more info:
  6. //
  7. // Partner API Doc: http://www.salesforce.com/us/developer/docs/api/index.htm
  8. // PHP Toolkit: http://wiki.developerforce.com/index.php/PHP_Toolkit
  9. //
  10. // If these required parameters are not provided, you will be redirected to index.php,
  11. // which has a form to conveniently provide these parameters to any script in this folder.
  12. if (!isset($_REQUEST["partnerApiEndpoint"]) || !isset($_REQUEST["sessionId"])) {
  13. header("Location: index.php") ;
  14. }
  15. // STEP 2: INITIALIZE THE BULK API CLIENT
  16. require_once '../BulkApiClient.php';
  17. $myBulkApiConnection = new BulkApiClient($_REQUEST["partnerApiEndpoint"], $_REQUEST["sessionId"]);
  18. $myBulkApiConnection->setLoggingEnabled(true); //optional, but using here for demo purposes
  19. $myBulkApiConnection->setCompressionEnabled(true); //optional, but recommended. defaults to true.
  20. // STEP 3: CREATE A NEW JOB
  21. // create in-memory representation of the job
  22. $job = new JobInfo();
  23. $job->setObject("Contact");
  24. $job->setOpertion("insert");
  25. $job->setContentType("CSV");
  26. $job->setConcurrencyMode("Parallel"); //can also set to Serial
  27. //$job->setExternalIdFieldName("My_Contact_External_Id"); //used with Upsert operations
  28. //$job->setAssignmentRuleId("01Q60000000EPDU"); //optional for objects that support Assignment Rules
  29. //send the job to the Bulk API and pass back returned JobInfo to the same variable
  30. $job = $myBulkApiConnection->createJob($job);
  31. // STEP 4. CREATE A NEW BATCH
  32. //prep the data. normally this would be loaded from a file,
  33. //but showing in plain text for demo purposes
  34. $csvData = "\"FirstName\",\"LastName\",\"Email\"\n" . //header row
  35. "\"Tom\",\"Collins\",\"tom@collins.com\"\n" . //data row #1
  36. "\"Mary\",\"Martini\",\"#N/A\"\n"; //data row #2 - using #N/A to null out .Email
  37. $batch = $myBulkApiConnection->createBatch($job, $csvData);
  38. //add more and more batches.... (here, we will only do one)
  39. // STEP 5. CLOSE THE JOB
  40. $myBulkApiConnection->updateJobState($job->getId(), "Closed");
  41. // STEP 6: MONITOR BATCH STATUS UNTIL DONE
  42. while($batch->getState() == "Queued" || $batch->getState() == "InProgress") {
  43. $batch = $myBulkApiConnection->getBatchInfo($job->getId(), $batch->getId());
  44. sleep(5); //wait for 5 seconds before polling again. in the real world, probably make this exponential as to not ping the server so much
  45. }
  46. // STEP 7: GET BATCH RESULTS
  47. $batchResults = $myBulkApiConnection->getBatchResults($job->getId(), $batch->getId());
  48. // PRINT EVERYTHING THAT HAPPENED ABOVE
  49. print "<pre>" .
  50. "PHP BULK API CLIENT SAMPLE CODE OUTPUT\n" .
  51. "This is the output of the PHP Bulk API Client Sample Code. View the source code for step-by-step explanations.\n\n";
  52. print "== CSV DATA == \n" . htmlspecialchars($csvData) . "\n\n";
  53. print "== BATCH RESULTS == \n" . htmlspecialchars($batchResults) . "\n\n";
  54. print "== CLIENT LOGS == \n" . $myBulkApiConnection->getLogs() . "\n\n";
  55. $myBulkApiConnection->clearLogs(); //clear log buffer
  56. print "</pre>";
  57. ?>