Created
May 24, 2012 21:40
-
-
Save metadaddy/2784403 to your computer and use it in GitHub Desktop.
Calling Force.com SOAP API on behalf of a portal user
This file contains 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 ('soapclient/SforcePartnerClient.php'); | |
function get_resource($url, $access_token) { | |
$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_FOLLOWLOCATION, true); | |
$json_response = curl_exec($curl); | |
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200) { | |
echo "URL is $url\n"; | |
echo "Status is " . curl_getinfo($curl, CURLINFO_HTTP_CODE) . | |
" curl_errno() " .curl_errno($curl). | |
", curl_error() ".curl_error($curl). | |
", response ".$json_response."\n"; | |
} | |
curl_close($curl); | |
return json_decode($json_response, true); | |
} | |
define("SF_USERNAME", getenv("USERNAME")); | |
define("SF_PASSWORD", getenv("PASSWORD")); | |
define("SF_ORGID", getenv("ORGID")); | |
define("SF_PORTALID", getenv("PORTALID")); | |
$mySforceConnection = new SforcePartnerClient(); | |
$mySforceConnection->createConnection("soapclient/partner.wsdl.xml"); | |
$mySforceConnection->setLoginScopeHeader(new LoginScopeHeader(SF_ORGID, SF_PORTALID)); | |
$login = $mySforceConnection->login(SF_USERNAME, SF_PASSWORD); | |
echo "Login result:\n"; | |
print_r($login); | |
// Get info on me | |
$identityServiceUrl = 'https://login.salesforce.com/id/'.$login->userInfo->organizationId.'/'.$login->userInfo->userId.'?version=latest'; | |
$id = get_resource($identityServiceUrl, $login->sessionId); | |
echo "My id:\n"; | |
print_r($id); | |
$query = "SELECT Subject FROM Case WHERE OwnerId = '".$login->userInfo->userId."'"; | |
// Get my cases via the SOAP API | |
$response = $mySforceConnection->query($query); | |
echo "My cases via SOAP:\n"; | |
print_r($response); | |
// Get my cases via the REST API | |
$queryUrl = $id["urls"]["rest"]."query?q=".urlencode($query); | |
$response = get_resource($queryUrl, $login->sessionId); | |
echo "My cases via REST:\n"; | |
print_r($response); | |
?> |
This file contains 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 portalauth.php | |
Login result: | |
stdClass Object | |
( | |
[metadataServerUrl] => https://na14-api.salesforce.com/services/Soap/m/20.0/00Dd0000000aaaa | |
[passwordExpired] => | |
[sandbox] => | |
[serverUrl] => https://na14-api.salesforce.com/services/Soap/u/20.0/00Dd0000000aaaa | |
[sessionId] => 00Dd0000000aaaa!123456789012345678901234567890123456789012345678901234567890123456789012345678901234 | |
[userId] => 005d0000000AAAAAA | |
[userInfo] => stdClass Object | |
( | |
[accessibilityMode] => | |
[currencySymbol] => $ | |
[orgDefaultCurrencyIsoCode] => USD | |
[orgDisallowHtmlAttachments] => | |
[orgHasPersonAccounts] => | |
[organizationId] => 00Dd0000000aaaaAAA | |
[organizationMultiCurrency] => | |
[organizationName] => Devorg | |
[profileId] => 00ed0000000BBBBBBB | |
[roleId] => | |
[userDefaultCurrencyIsoCode] => | |
[userEmail] => user@example.com | |
[userFullName] => Pat Patterson | |
[userId] => 005d0000000AAAAAA | |
[userLanguage] => en_US | |
[userLocale] => en_US | |
[userName] => user@example.com | |
[userTimeZone] => America/Los_Angeles | |
[userType] => CSPLitePortal | |
[userUiSkin] => PortalDefault | |
) | |
) | |
My id: | |
Array | |
( | |
[id] => https://login.salesforce.com/id/00Dd0000000aaaaAAA/005d0000000AAAAAA | |
[asserted_user] => 1 | |
[user_id] => 005d0000000AAAAAA | |
[organization_id] => 00Dd0000000aaaaAAA | |
[username] => user@example.com | |
[nick_name] => user | |
[display_name] => Pat Patterson | |
[email] => user@example.com | |
[first_name] => Pat | |
[last_name] => Patterson | |
[status] => Array | |
( | |
[created_date] => | |
[body] => | |
) | |
[photos] => Array | |
( | |
[picture] => https://na14.salesforce.com/img/userprofile/default_profile_200.png | |
[thumbnail] => https://na14.salesforce.com/img/userprofile/default_profile_45.png | |
) | |
[urls] => Array | |
( | |
[enterprise] => https://na14.salesforce.com/services/Soap/c/24.0/00Dd0000000aaaa | |
[metadata] => https://na14.salesforce.com/services/Soap/m/24.0/00Dd0000000aaaa | |
[partner] => https://na14.salesforce.com/services/Soap/u/24.0/00Dd0000000aaaa | |
[rest] => https://na14.salesforce.com/services/data/v24.0/ | |
[sobjects] => https://na14.salesforce.com/services/data/v24.0/sobjects/ | |
[search] => https://na14.salesforce.com/services/data/v24.0/search/ | |
[query] => https://na14.salesforce.com/services/data/v24.0/query/ | |
[recent] => https://na14.salesforce.com/services/data/v24.0/recent/ | |
[profile] => https://na14.salesforce.com/005d0000000AAAAAA | |
[feeds] => https://na14.salesforce.com/services/data/v24.0/chatter/feeds | |
[groups] => https://na14.salesforce.com/services/data/v24.0/chatter/groups | |
[users] => https://na14.salesforce.com/services/data/v24.0/chatter/users | |
[feed_items] => https://na14.salesforce.com/services/data/v24.0/chatter/feed-items | |
) | |
[active] => 1 | |
[user_type] => CSP_LITE_PORTAL | |
[language] => en_US | |
[locale] => en_US | |
[utcOffset] => -28800000 | |
[last_modified_date] => 2012-05-24T19:41:50.000+0000 | |
) | |
My cases via SOAP: | |
QueryResult Object | |
( | |
[queryLocator] => | |
[done] => 1 | |
[records] => Array | |
( | |
[0] => SObject Object | |
( | |
[type] => Case | |
[fields] => stdClass Object | |
( | |
[Subject] => Are we nearly there yet? | |
) | |
) | |
) | |
[size] => 1 | |
) | |
My cases via REST: | |
Array | |
( | |
[totalSize] => 1 | |
[done] => 1 | |
[records] => Array | |
( | |
[0] => Array | |
( | |
[attributes] => Array | |
( | |
[type] => Case | |
[url] => /services/data/v24.0/sobjects/Case/500d0000003QWB7AAO | |
) | |
[Subject] => Are we nearly there yet? | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment