<?php
$data = array("grant_type" => "password", "client_id" => "*********************************","client_secret"=>"************","username" =>"*********","password" => "**************");
$data_string = json_encode($data);   
$url='https://login.salesforce.com/services/oauth2/token';                                                                                                             
$ch = curl_init('https://login.salesforce.com/services/oauth2/token');     

curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);                                                                 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
curl_setopt($ch, CURLOPT_HEADER,true);                                                                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);  
curl_setopt($ch,CURLOPT_VERBOSE,true);                                                                   
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

if($result === false)
{
    echo 'Curl error: ' . curl_error($ch);
}
else
{
    echo 'Operation completed without any errors';
}


$info = curl_getinfo($ch);
echo curl_getinfo($ch);
echo $info['access_token'];
echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$result1= file_get_contents($url);
var_dump(json_decode($result1, true));
}

?>

Try using these parameters

  1. The content-type HTTP header should be application/x-www-form-urlencoded
  2. The grant_type form parameter should be password (all lower case)

You are setting the POSTFIELDS to $params but you are not ever setting $params. You set the values to the $data_String. Many other problems as well I think but I am not an expert in php

Here is the relevant parts of what I have used that works just fine (the require is just the variables I use:

<?php
require_once 'configPWFlow.php';

session_start();

$token_url = LOGIN_URI . "/services/oauth2/token";


$params = "grant_type=password"
    . "&client_id=" . CLIENT_ID
    . "&client_secret=" . CLIENT_SECRET
    . "&username=" . USERNAME
    . "&password=" . PASSWORD;

$curl = curl_init($token_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);
...
?>
  • Thanks for you answer! I have few questions regarding this. 1> require_once will be the name of my PHP? 2> the format of token url I am using is : $url='login.salesforce.com/services/oauth2/token'; is this correct? 3> also I did not get the dots(.) you entered in params as the format is different as of what I am using. Can we tell me the exact format? I am trying to post some data to salesforce. Is it different from regular POST? – shweta Jul 5 '16 at 15:38
  • @shweta - the single dot is a concatenation operator for strings in php. The ... is just a placeholder for the rest of your code. The require once in my example is just a file with my defines. – Eric Jul 5 '16 at 16:34

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.