function establish(ConnectionConfiguration $connConfig)
 {
     $asyncConnection = new BulkApiClient($this->buildEndpoint($connConfig), $connConfig->getSessionId());
     $asyncConnection->setCompressionEnabled(WorkbenchConfig::get()->value("enableGzip"));
     $asyncConnection->setIncludeSessionCookie(WorkbenchConfig::get()->value("includeSessionCookie"));
     $asyncConnection->setUserAgent(getWorkbenchUserAgent());
     $asyncConnection->setExternalLogReference($_SESSION['restDebugLog']);
     //TODO: maybe replace w/ its own log?? //TODO: move into ctx
     $asyncConnection->setLoggingEnabled(WorkbenchConfig::get()->value("debug"));
     $asyncConnection->setProxySettings(getProxySettings());
     return $asyncConnection;
 }
 public function batchQuery($object, $fields, $startDate = null, $fh = null)
 {
     $this->initSession();
     $myBulkApiConnection = new BulkApiClient($this->session->serverUrl, $this->session->sessionId);
     $myBulkApiConnection->setLoggingEnabled(false);
     $myBulkApiConnection->setCompressionEnabled(true);
     // create in-memory representation of the job
     $job = new JobInfo();
     $job->setObject($object);
     $job->setOpertion('query');
     $job->setContentType('CSV');
     $job->setConcurrencyMode('Parallel');
     $soql = "SELECT " . implode(',', $fields) . " FROM {$object}";
     if ($startDate != null) {
         $soql .= " WHERE LastModifiedDate >= {$startDate}";
     }
     echo 'Creating job...';
     $job = $myBulkApiConnection->createJob($job);
     echo 'ok' . PHP_EOL;
     echo 'Creating batch...';
     $batch = $myBulkApiConnection->createBatch($job, $soql);
     echo 'ok' . PHP_EOL;
     echo 'Closing job...';
     $myBulkApiConnection->updateJobState($job->getId(), 'Closed');
     echo 'ok' . PHP_EOL;
     $sleepTime = 4;
     echo 'Waiting for job to complete...';
     while ($batch->getState() == 'Queued' || $batch->getState() == 'InProgress') {
         // poll Salesforce for the status of the batch
         sleep($sleepTime *= 1.1);
         echo ".";
         $batch = $myBulkApiConnection->getBatchInfo($job->getId(), $batch->getId());
     }
     echo 'ok' . PHP_EOL;
     // get status of batches
     echo "Retrieving results...";
     $resultList = $myBulkApiConnection->getBatchResultList($job->getId(), $batch->getId());
     // retrieve queried data
     foreach ($resultList as $resultId) {
         $myBulkApiConnection->getBatchResult($job->getId(), $batch->getId(), $resultId, $fh);
     }
     echo 'ok' . PHP_EOL;
     if (isset($fh)) {
         $preview = stream_get_contents($fh, 32, 0);
         rewind($fh);
         if (strcasecmp($preview, 'Records not found for this query') == 0 || trim($preview) == false) {
             // return false if no records returned
             return false;
         } else {
             return true;
         }
     }
 }
// STEP 1: OBTAIN SESSION ID AND ENDPOINT FROM PARTNER API. REPLACE WITH YOUR ENDPOINT AND SESSION ID.
// For demo purposes, these can just be GET parameters on this page, but should be
// obtained from the login() call using the Force.com Partner API with a username and password.
// In PHP, it is recommended to use the PHP Toolkit to call the Partner API. For more info:
//
// Partner API Doc: http://www.salesforce.com/us/developer/docs/api/index.htm
// PHP Toolkit: http://wiki.developerforce.com/index.php/PHP_Toolkit
//
// If these required parameters are not provided, you will be redirected to index.php,
// which has a form to conveniently provide these parameters to any script in this folder.
if (!isset($_REQUEST["partnerApiEndpoint"]) || !isset($_REQUEST["sessionId"])) {
    header("Location: index.php");
}
// STEP 2: INITIALIZE THE BULK API CLIENT
require_once '../BulkApiClient.php';
$myBulkApiConnection = new BulkApiClient($_REQUEST["partnerApiEndpoint"], $_REQUEST["sessionId"]);
$myBulkApiConnection->setLoggingEnabled(true);
//optional, but using here for demo purposes
$myBulkApiConnection->setCompressionEnabled(true);
//optional, but recommended. defaults to true.
// STEP 3: CREATE A NEW JOB
// create in-memory representation of the job
$job = new JobInfo();
$job->setObject("Contact");
$job->setOpertion("insert");
$job->setContentType("XML");
$job->setConcurrencyMode("Parallel");
//can also set to Serial
//$job->setExternalIdFieldName("My_Contact_External_Id");     //used with Upsert operations
//$job->setAssignmentRuleId("01Q60000000EPDU");               //optional for objects that support Assignment Rules
//send the job to the Bulk API and pass back returned JobInfo to the same variable
// STEP 1: OBTAIN SESSION ID AND ENDPOINT FROM PARTNER API. REPLACE WITH YOUR ENDPOINT AND SESSION ID.
// For demo purposes, these can just be GET parameters on this page, but should be
// obtained from the login() call using the Force.com Partner API with a username and password.
// In PHP, it is recommended to use the PHP Toolkit to call the Partner API. For more info:
//
// Partner API Doc: http://www.salesforce.com/us/developer/docs/api/index.htm
// PHP Toolkit: http://wiki.developerforce.com/index.php/PHP_Toolkit
//
// If these required parameters are not provided, you will be redirected to index.php,
// which has a form to conveniently provide these parameters to any script in this folder.
if (!isset($_REQUEST["partnerApiEndpoint"]) || !isset($_REQUEST["sessionId"])) {
    header("Location: index.php");
}
// STEP 2: INITIALIZE THE BULK API CLIENT
require_once '../BulkApiClient.php';
$myBulkApiConnection = new BulkApiClient($_REQUEST["partnerApiEndpoint"], $_REQUEST["sessionId"]);
$myBulkApiConnection->setLoggingEnabled(false);
// optional
$myBulkApiConnection->setCompressionEnabled(true);
//optional, but recommended. defaults to true.
// STEP 3: CREATE A NEW JOB
// create in-memory representation of the job
$job = new JobInfo();
// $job->setObject("Contact");
$job->setObject("Contact");
$job->setOpertion("query");
$job->setContentType("CSV");
$job->setConcurrencyMode("Parallel");
//can also set to Serial
//send the job to the Bulk API and pass back returned JobInfo to the same variable
$job = $myBulkApiConnection->createJob($job);