PHP – Use oAuth to consume SalesForce SOAP API


Ask A Question





Login To post a question

I’m trying to use oAuth to consume the SOAP API via PHP.

I have successfully obtained both my instance URL & my access token. However, it would appear I cannot use the login() method with oAuth. I’ve tried setting the session headers manually, but I get an error:
UNKNOWN_EXCEPTION: Destination URL not reset. The URL returned from login must be set in the SforceService

Could someone please show me how I could set the API sessionId header to use the access_token I got from my oAuth flow instead? I saw a couple links from long ago but they’re all broken/outdated.

Here’s my existing code:

 $access_token = isset($_GET['access_token']);
$instance_url = isset($_GET['instance_url']);
If(!$access_token) {
    header('Location: ********************');
}
try {
    define("SOAP_CLIENT_BASEDIR", "../soapclient");
require_once (SOAP_CLIENT_BASEDIR.'/SforcePartnerClient.php');
require_once (SOAP_CLIENT_BASEDIR.'/SforceHeaderOptions.php');

    $mySforceConnection = new SforcePartnerClient();
    $mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/partner.wsdl.xml');
    $mySforceConnection->setSessionHeader('sessionId',$access_token);
    $mySforceConnection->setSessionHeader('serverUrl','https://<<instance>>/services/Soap/u/27.0/<<orgID>>');
$query = "select id from user where name='chris miller'";
$response = $mySforceConnection->query($query);

} catch (Exception $e) {
    print_r($mySforceConnection->getLastRequest());
    echo '<p style="margin-left:50px;color:red;font-weight:bold;">' . $e->faultstring . '</p>';
}

The only example I could find used a deprecated method attach() that would essentially append the sessionId & server URL to your session header. But I couldn’t find that method anywhere

For more info please click herehttps://stackoverflow.com/q/50842384



Have a question or need a custom quote?
info@smartinfocare.com