Creating a SalesForce Guide Using the REST API in PHP

Iโ€™ve been trying to create a SalesForce REST API guide for the past few days, but I canโ€™t make it work for life. I can get the access token without problems, but from there, to create leadership, I was completely out of luck.

I see everything in all the documentation:

curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d @newaccount.json" 

How would this be done in PHP curl? I tried and tried, but I had no luck.

This is how I got the access token:

 $ch = curl_init(); // set URL options curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=".CONSUMER_KEY."&client_secret=".CONSUMER_SECRET."&username=".USERNAME."&password=".USERPASS.SECURITY_TOKEN); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // grab HTML $data = curl_exec($ch); $data = json_decode($data, true); $code = $data['access_token']; curl_close($ch); 

I tried to do something similar after this code, however I was out of luck.

 $token_url = LOGIN_BASE_URL.'/services/oauth2/token'; $post_fields = array( 'code' => $code, 'grant_type' => 'authorization_code', 'client_id' => CONSUMER_KEY, 'client_secret' => CONSUMER_SECRET, ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $token_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); $token_request_body = curl_exec($ch) 

I just need to figure out how to create leaders in SalesForce, I have no idea where to go from here. Any help would be greatly appreciated, as I cannot find decent documentation anywhere that helps me.

+6
source share
1 answer

create a demo account so you get started:

 function create_account($name, $instance_url, $access_token) { $url = "$instance_url/services/data/v20.0/sobjects/Account/"; $content = json_encode(array("Name" => $name)); $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)); } echo "HTTP status $status creating account<br/><br/>"; curl_close($curl); $response = json_decode($json_response, true); $id = $response["id"]; echo "New record id $id<br/><br/>"; return $id; } 
+6
source

Source: https://habr.com/ru/post/959191/


All Articles