function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
joestuartjoestuart 

Multiple connections to SFDC via PHP toolkit - SOAP

Hi

I have made a wordpress module that creates a connection to Salesforce. The modules all work fine when they are the only widgets on the page.

The problem is that I have multiple modules on the page and they are all trying to start a new connection and it only allows for one connection.
try {
  $mySforceConnection = new SforceEnterpriseClient();
 
  $mySoapClient = $mySforceConnection->createConnection( $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/widgets/enterprise.wsdl.xml");
 
  $mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);
So my question is, what is the best way to make a connection under my circumstances? Should I make a connection in the first module and then on the following modules check to see if a connection is established and if so get session Id?  How should this be done securely  With PHP super globals "$_GET"?  

Thank you.
Best Answer chosen by joestuart
pconpcon
I think your problem is you are saving the WSDL to the session but it's an empty variable.  Try updating your code to this
 
<?php

session_start();

define("SOAP_CLIENT_BASEDIR", "../soapclient");
require_once($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceEnterpriseClient.php");
require_once($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/samples/userAuth.php");


$mySforceConnection = new SforceEnterpriseClient();
  
$wsdl = $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/enterprise.wsdl.xml";
$mySoapClient = $mySforceConnection->createConnection($wsdl);
  
$mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);

$_SESSION['location'] = $mySforceConnection->getLocation();
$_SESSION['sessionId'] = $mySforceConnection->getSessionId();
$_SESSION['wsdl'] = $wsdl;

All Answers

pconpcon
I think you want to use the built in session managment inside of the PHP toolkit.  Take a look here [1] for the "Session Management" section.  Let me know if that helps.

[1] https://developer.salesforce.com/page/Getting_Started_with_the_Force.com_Toolkit_for_PHP
joestuartjoestuart

Thank you for your reply pcon.


It is definitely what I was after.  I have managed to successfully start the session but for the subsequent pages I am getting this error: 
 

Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient() [<a href='soapclient.soapclient'>soapclient.soapclient</a>]: 'uri' option is required in nonWSDL mode in /home/experien/public_html/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceBaseClient.php:64 Stack trace: #0 /home/experien/public_html/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceBaseClient.php(64): SoapClient->SoapClient(NULL, Array) #1 /home/experien/public_html/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceBaseClient.php(133): SforceBaseClient->getSoapClient(NULL, Array) #2 /home/experien/public_html/wp-content/themes/devoll/widgets/SFDC-SOAP/test/queryDates.php(12): SforceBaseClient->createConnection(NULL) #3 /home/experien/public_html/wp-content/themes/devoll/widgets/modules/program-dates/view.php(18): include('/home/experien/...') #4 /home/experien/public_html/wp-content/themes/devoll/widgets/base.php(53): include('/home/experien/...') #5 /home/experien/public_html/wp-includes/widgets.php(344): in /home/experien/public_html/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceBaseClient.php on line 64

 
pconpcon
Can you provide the code you are using on your session start and subsequent pages?
joestuartjoestuart
Here is my code for my Session start - 
<?php

session_start();

// [Code to create the connection...]
define("SOAP_CLIENT_BASEDIR", "../soapclient");
//require_once (SOAP_CLIENT_BASEDIR.'/SforceEnterpriseClient.php');
require_once($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceEnterpriseClient.php");
require_once($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/samples/userAuth.php");


$mySforceConnection = new SforceEnterpriseClient();
  
  $mySoapClient = $mySforceConnection->createConnection( $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/enterprise.wsdl.xml");
  
  $mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);

// Now we can save the connection info for the next page
$_SESSION['location'] = $mySforceConnection->getLocation();
$_SESSION['sessionId'] = $mySforceConnection->getSessionId();
$_SESSION['wsdl'] = $wsdl;

And the subsequent pages
<?php

session_start();

// Retrieve session attributes
$location = $_SESSION['location'];
$sessionId = $_SESSION['sessionId'];
$wsdl = $_SESSION['wsdl'];

// Use SforceEnterpriseClient or SforcePartnerClient as appropriate
$mySforceConnection = new SforceEnterpriseClient();
$mySforceConnection->createConnection($wsdl);
$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);

Thank you.
pconpcon
I think your problem is you are saving the WSDL to the session but it's an empty variable.  Try updating your code to this
 
<?php

session_start();

define("SOAP_CLIENT_BASEDIR", "../soapclient");
require_once($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/SforceEnterpriseClient.php");
require_once($_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/samples/userAuth.php");


$mySforceConnection = new SforceEnterpriseClient();
  
$wsdl = $_SERVER['DOCUMENT_ROOT']."/wp-content/themes/devoll/widgets/SFDC-SOAP/soapclient/enterprise.wsdl.xml";
$mySoapClient = $mySforceConnection->createConnection($wsdl);
  
$mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);

$_SESSION['location'] = $mySforceConnection->getLocation();
$_SESSION['sessionId'] = $mySforceConnection->getSessionId();
$_SESSION['wsdl'] = $wsdl;
This was selected as the best answer
joestuartjoestuart
That did the Trick!
Thank you Pcon!
pconpcon
No problem, glad I could help!  You did force me to use / look at PHP for the first time in a while :)