Last active
January 8, 2019 07:19
-
-
Save thanhtungka91/91a4820ca2b8352c313db7e6992b5ecd to your computer and use it in GitHub Desktop.
for example login salesforce
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 | |
require_once("../require.php"); | |
class SC_Helper_SaleForce | |
{ | |
// login and return access_token, url | |
public function login() | |
{ | |
// create class Log for dev, only in dev enviroment | |
// $this->log_dev = new Log_Dev_Salesforce(); | |
// $this->log_dev->logging_debug("","login",""); | |
// for get client_id check page | |
//https://ap5.salesforce.com/app/mgmt/forceconnectedapps/forceAppDetail.apexp?applicationId=06P7F000000GtPF&id=0Ci7F00000000Jh | |
$params = "grant_type=password" | |
. "&client_id=".SALEFORCE_CLIENT_ID | |
. "&client_secret=".SALEFORCE_CLIENT_SECRET | |
. "&username=".SALEFORCE_USERNAME | |
. "&password=".SALEFORCE_PASSWORD ; | |
$curl_result = $this->setup_url(SALEFORCE_LOGIN_URL,"POST", $params,null); | |
$response = $curl_result["response"]; | |
$status = $curl_result["status_code"]; | |
var_dump($curl_result); | |
die(); | |
if ( $status != 200 ) { | |
$error = json_encode($curl_result); | |
$this->logging_err($response); | |
die("Error: check again username, passoword or url, maybe you didnt combine password with keysecret"); | |
return 0; | |
} | |
return $response; | |
} | |
// using when the customer only provide code (no password, username) and need to get the refresh_token | |
public function first_freshtoken(){ | |
// first you need to run browser to get code | |
// https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=3MVG9d8..z.hDcPIPaNB1ZAck34yy_Ot5hYJIv7tVjrVYZTPBP.Yi_h6OewxSSd6pqNqWvzK2Vsy7_YpfX4KN&redirect_uri=https://login.salesforce.com/services/oauth2/success | |
// then get the code and update to config file | |
$curl = curl_init(SALEFORCE_LOGIN_URL); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
$json_response = curl_exec($curl); | |
$params = "grant_type=authorization_code" | |
. "&client_id=".SALEFORCE_CLIENT_ID | |
. "&client_secret=".SALEFORCE_CLIENT_SECRET | |
. "&code=".SALEFORCE_CODE | |
. "&redirect_uri=".SALEFORCE_DIRECT_URI ; | |
$curl_result = $this->setup_url(SALEFORCE_LOGIN_URL,"POST", $params,null); | |
print_r($curl_result); | |
// update instance url, accesstoken | |
return 0; | |
} | |
function show_records($instance_url,$access_token, $object) { | |
$query = "SELECT Name from ".$object; | |
$url = "$instance_url/services/data/v20.0/query?q=" . urlencode($query); | |
$curl_result = $this->setup_url($url, "GET", null,$access_token); | |
$status = $curl_result["status_code"]; | |
// check status code later | |
if ( $status != 201) { | |
$error = json_encode($curl_result); | |
var_dump($curl_result); | |
return 0; | |
} | |
else{ | |
$response = $curl_result["response"]; | |
$total_size = $response['totalSize']; | |
echo "$total_size record(s) returned<br/><br/>"; | |
foreach ((array) $response['records'] as $record) { | |
echo $record['Id'] . ", " . $record['Name'] . "<br/>"; | |
echo json_encode($record); | |
echo "<br/>"; | |
} | |
echo "<br/>"; | |
print_r($curl_result); | |
return $response; | |
} | |
} | |
//create new | |
public function create_record($instance_url, $access_token, $object, $sql) { | |
$url = "$instance_url/services/data/v20.0/sobjects/$object/"; | |
$content = $sql; | |
$curl_result = $this->setup_url($url,"POST", $content,$access_token); | |
$response = $curl_result["response"]; | |
$status = $curl_result["status_code"]; | |
if($status!=201 ){ | |
$error = json_encode($curl_result); | |
$this->logging_err($error); | |
die("Error: has an account when create new"); | |
} | |
else{ | |
$id = $response["id"]; | |
} | |
echo "New record id $id<br/><br/>"; | |
return $id; | |
} | |
// update record | |
public function update_record( $instance_url, $access_token, $object, $id, $sql) { | |
// enable only for dev enviroment | |
// $this->log_dev->logging_debug("","update, table: ".$object,""); | |
$url = "$instance_url/services/data/v20.0/sobjects/$object/$id"; | |
$curl_result = $this->setup_url($url,"PATCH", $sql, $access_token); | |
$status = $curl_result["status_code"]; | |
if($status!=204){ | |
$error = json_encode($curl_result); | |
$this->logging_err($error); | |
die("Error: has nad issue when update"); | |
} | |
echo "HTTP status $status updating account<br/><br/>"; | |
} | |
// delete record | |
public function delete_record( $instance_url, $access_token,$object,$id) { | |
$url = "$instance_url/services/data/v20.0/sobjects/$object/$id"; | |
$curl_result = $this->setup_url($url,"DELETE", null, $access_token); | |
$status = $curl_result["status_code"]; | |
if($status!=204){ | |
$error = json_encode($curl_result); | |
$this->logging_err($error); | |
die("Error: has nad issue when delete"); | |
} | |
echo "HTTP status $status delete account<br/><br/>"; | |
} | |
// base setup curl | |
public function setup_url($url, $method, $sql, $access_token){ | |
$content = $sql; | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
// if login function, no require access_token | |
if($access_token!=null){ | |
$content = json_encode($content); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, | |
array("Authorization: OAuth $access_token", | |
"Content-type: application/json")); | |
} | |
// define method | |
if($method == "POST"){ | |
// print_r("post method"); | |
curl_setopt($curl, CURLOPT_POST, true); | |
}else if ($method == "PATCH") { | |
// print_r("update method"); | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); | |
} else if($method == "DELETE"){ | |
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); | |
} | |
if($sql!=null){ | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); | |
} | |
$json_response = curl_exec($curl); | |
// get status of the response | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if($status==401){ | |
$access_token = $this->refresh_token(); | |
if($access_token!=null){ | |
curl_setopt($curl, CURLOPT_HTTPHEADER, | |
array("Authorization: OAuth $access_token", | |
"Content-type: application/json")); | |
} | |
$json_response = curl_exec($curl); | |
} | |
curl_close($curl); | |
if($json_response!=""){ | |
$response = json_decode($json_response, true); | |
}else{ | |
$response = ""; | |
} | |
$result = array( | |
"status_code" => $status, | |
"response" => $response, | |
"access_token" => $access_token | |
); | |
return $result; | |
} | |
// refresh token if token expried | |
public function refresh_token(){ | |
$params = "grant_type=refresh_token" | |
. "&client_id=".SALEFORCE_CLIENT_ID | |
. "&client_secret=".SALEFORCE_CLIENT_SECRET | |
. "&refresh_token=".SALEFORCE_REFRESH_TOKEN | |
; | |
$curl = curl_init(SALEFORCE_LOGIN_URL); | |
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); | |
// get status of the response | |
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
curl_close($curl); | |
$response = json_decode($json_response, true); | |
$access_token = $response["access_token"]; | |
if(!$access_token){ | |
$error = "Has an error during refresh token"; | |
$this->logging_err($error); | |
} | |
return $access_token; | |
} | |
//for log error | |
// public function logging_err($msg){ | |
// $fileName = "salesforce_error.txt"; | |
// $fp = fopen($fileName, "a"); | |
// if ( !$fp ) { | |
// die("please check a permsion"); | |
// } | |
// $str_log = stream_get_contents($fp); | |
// // append date/time to message | |
// $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg; | |
// // write string | |
// fwrite($fp, "\n".$str); | |
// if ($fwrite === false) { | |
// die("cannot write a logs"); | |
// } | |
// fclose($fp); | |
// } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment