Created
May 25, 2016 08:23
-
-
Save osvik/26f749b56e9ce48176f8a8eb84c93e7d to your computer and use it in GitHub Desktop.
Connect to Universe
This file contains 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 | |
class Universe { | |
/** | |
* Logins in Salesforce for the API | |
* | |
* @return array Returns an associative array with 'access_token' and other | |
*/ | |
public static function login() { | |
$loginurl = "https://login.salesforce.com/services/oauth2/token"; | |
$params = "grant_type=password" | |
. "&client_id=" . CLIENT_ID | |
. "&client_secret=" . CLIENT_SECRET | |
. "&username=" . USER_NAME | |
. "&password=" . PASSWORD; | |
$curl = curl_init($loginurl); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); | |
$json_response = curl_exec($curl); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ( $status != 200 ) { | |
// die("Error: call to URL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); | |
} | |
curl_close($curl); | |
$response = json_decode( $json_response, true ); | |
return $response; | |
} | |
/** | |
* Inserts record in Salesforce staging object | |
* | |
* @param Array $record Formated record | |
* @param String $instance_url Instance URL as returned from the login method | |
* @param String $access_token Access Token as returned from the login method | |
*/ | |
public static function insertInStagingObject( $record, $instance_url, $access_token) { | |
$url = "$instance_url/services/data/v20.0/sobjects/s360aie__Staging__c/"; | |
$content = json_encode( $record ); | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, | |
array("Authorization: OAuth $access_token", | |
"Content-type: application/json")); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); | |
$json_response = curl_exec($curl); | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ( $status != 201 ) { | |
// die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl)); | |
} | |
curl_close($curl); | |
$response = json_decode($json_response, true); | |
return $response; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment