具有自动登录功能的Salesforce php REST API

pri*_*gue 6 php authentication api rest salesforce

我现在正把这头发拖过来.我遵循了以下示例:

http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php

但是这里用户被发送到登录表单并需要登录.相反,我想在我的代码中发布该数据,而不是让用户登录,但我的应用程序会自动执行此操作.

如果有人可以发布一个关于如何使用oAuth进行操作的示例,我会非常感激,因为我并不急于使用那个膨胀的SOAP实现.

干杯啦!

pri*_*gue 12

似乎经过一些修补我的尝试取得了成功::

$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);

echo $json_response;
Run Code Online (Sandbox Code Playgroud)

现在剩下要做的就是将该响应中的access_token&instance_url存储到会话变量中,并解决我们的对象问题.

我希望以上帮助其他有类似问题的人.

  • 谢谢你的代码!在某些设置中,我发现您需要生成安全令牌,然后将其附加到密码,例如"&password =".密码 SECURITY_TOKEN (2认同)