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
piyush parmarpiyush parmar 

salesfroce connection using PHP toolkit

I am fairly new using PHP with Salesforce, I'm not  able to creat successfully connection.

Here is the code:

 

   ini_set("soap.wsdl_cache_enabled", "0");
    // Include the PHP Toolkit
    require_once('soapclient/SforcePartnerClient.php');
    //require_once('SforceHeaderOptions.php');
 
    // Login
    $sfdc = new SforcePartnerClient();
    $SoapClient = $sfdc->createConnection('Partner.wsdl.xml');
    $loginResult = false;
    $loginResult = $sfdc->login('test@g.com', 'test1234.tokan' );
 
    // Define constants for the web service. We'll use these later
    $parsedURL = parse_url($sfdc->getLocation());
    define ("_SFDC_SERVER_", substr($parsedURL['host'],0,strpos($parsedURL['host'], '.')));
    define ("_WS_NAME_", 'LeadWebService');
    define ("_WS_WSDL_", _WS_NAME_ . '.xml');
    define ("_WS_ENDPOINT_", 'https://' . _SFDC_SERVER_ . '.salesforce.com/services/wsdl/class/' . _WS_NAME_);
    define ("_WS_NAMESPACE_", 'http://soap.sforce.com/schemas/class/' . _WS_NAME_);
    echo "_WS_WSDL_". _WS_NAME_;
    // SOAP Client for Web Service
    $client = new SoapClient(_WS_WSDL_);
    $sforce_header = new SoapHeader(_WS_NAMESPACE_, "SessionHeader", array("sessionId" => $sfdc->getSessionId()));
    $client->__setSoapHeaders(array($sforce_header));
    // data to send into the web service

 

 

NOte:here all file at correct path.

 

Please let me know if wrong,

Or other way than also post ,

 

 

Thx

piyush

piyush parmarpiyush parmar

I also try using below code. but not able to connect

 

session_start();
    
    // Config
    $sforceUsername = 'test@g.com';
    $sforcePassword = 'test1234';
    $sforceSecurityToken = 'token';
    $sforceWsdl = 'partner.wsdl.xml';
        
    // Initialize Salesforce
    require_once 'soapclient/SforcePartnerClient.php';
    
    $sforceClient = new SforcePartnerClient();
    
  
        echo 'trying to connect to salesforce';
        $soapClient = $sforceClient->createConnection($sforceWsdl);
        echo 'connection to salesforce established';

    if(!empty($_SESSION['sforceLocation']) && !empty($_SESSION['sforceSessionId']))
    {
        // Re-initialize connection from session
        $sforceClient->setEndpoint($_SESSION['sforceLocation']);
        $sforceClient->setSessionHeader($_SESSION['sforceSessionId']);
    }
    else
    {  

        $sforceLogin = $sforceClient->login($sforceUsername,$sforcePassword.$sforceSecurityToken);
    }
    echo "sforceLogin". $sforceLogin;
    // Store SOAP client attributes for later use
    $_SESSION['sforceLocation'] = $sforceClient->getLocation();
    $_SESSION['sforceSessionId'] = $sforceClient->getSessionId();
    echo "sforceSessionId" .$_SESSION['sforceSessionId'] ;
    $contactQuery = "SELECT Id,Email FROM Lead where Email " ;
    $contactResponse = $sforceClient->query($contactQuery);
    echo "contactResponse".$contactResponse

 

 

thx,

piyush