我正在尝试登录我的Salesforce帐户并检索一些数据。到目前为止,我已经能够通过在Salesforce登录页面上提供用户名和密码登录Salesforce,然后重定向到我有一些echo
语句的数据页面。我想知道如何在PHP中设置用户名和密码,并成功登录Salesforce。我使用了Salesforce提供的这个文档,Interact with the Force.com REST API from PHP
请帮忙。
以下是我的config.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL ^ E_WARNING);
define("CLIENT_ID", "xxx");
define("CLIENT_SECRET", "xxx");
define("REDIRECT_URI", "https://xxxx/oauth_callback.php");
define("LOGIN_URI", "https://login.salesforce.com");
define("USERNAME","xxxx@xxx.com");
define("PASSWORD","*******");
define("SECURITY_TOKEN","xxxx");
?>
以下是我的oauth.php
<?php
require_once 'config.php';
$auth_url = LOGIN_URI
. "/services/oauth2/authorize?response_type=code&client_id="
. CLIENT_ID . "&redirect_uri=" . urlencode(REDIRECT_URI);
header('Location: ' . $auth_url);
?>
以下是我的oauth_callback.php
<?php
require_once 'config.php';
session_start();
$token_url = LOGIN_URI . "/services/oauth2/token";
$code = $_GET['code'];
if (!isset($code) || $code == "") {
die("Error - code parameter missing from request!");
}
$params = "code=" . $code
. "&grant_type=authorization_code"
. "&client_id=" . CLIENT_ID
. "&client_secret=" . CLIENT_SECRET
. "&username=" . USERNAME
. "&password=" . PASSWORD
. "&redirect_uri=" . urlencode(REDIRECT_URI);
$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);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
die("Error: call to token URL $token_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);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];
if (!isset($access_token) || $access_token == "") {
die("Error - access token missing from response!");
}
if (!isset($instance_url) || $instance_url == "") {
die("Error - instance URL missing from response!");
}
$_SESSION['access_token'] = $access_token;
$_SESSION['instance_url'] = $instance_url;
header( 'Location: demo_rest.php' ) ;
?>
以下是我的demo_rest.php
(显示输出)
$query = "SELECT Id FROM test__c limit 3";
$url = "$instance_url/services/data/v20.0/query?q=" . urlencode($query);
$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"));
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
$total_size = $response['totalSize'];
echo "$total_size record(s) returned<br/><br/>";
echo "<br/>";
print_r($json_response);
答案 0 :(得分:0)
问题是你无法在demo_rest.php文件中获取变量$ instance_id和$ access_token的值。
将demo_rest.php文件的代码替换为下面的代码。
<?php
session_start();
$access_token = $_SESSION['access_token'];
$instance_url = $_SESSION['instance_url'];
$query = "SELECT Id FROM test__c limit 3";
$url = "$instance_url/services/data/v20.0/query?q=" . urlencode($query);
$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"));
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
$total_size = $response['totalSize'];
echo "$total_size record(s) returned<br/><br/>";
echo "<br/>";
print_r($json_response);
?>
如果它不起作用,请给我留言。