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
Vishal Gaddi6Vishal Gaddi6 

error in Connection to salesforce

Hi

 

I am trying to connect php page with my salesforce account. I have found the following code which is saved as SF_Connect.class.php 

 

 

<?php

 

class SF_Connect {   

private static $soapClient;

        private static $wsdlMethod;
        const SALESFORCE_USER = 'username';
        const SALESFORCE_PASS = 'passwrd';
        const SALESFORCE_TOKEN = 'XuQVD3uYJ8Q7w2jsIGdEGgl3W';
        const IN_DEBUG_MODE = true;
        const ERROR_EMAIL = 'vishal_gaddi@rediffmail.com';
        const SF_SOAPCLIENT_DIR = 'sf-lib/toolkit/soapclient/';


        public static function connect($wsdlMethod = 'partner') {       

 

        if (self::$soapClient) return;
       if ( !$wsdlMethod || !in_array($wsdlMethod, array('partner', 'enterprise', 'metadata')) ) {           die(((self::IN_DEBUG_MODE) ? 'Bad wsdl requested' : 'Unable to connect to salesforce.com'));       

}
       try {           

# Create client and connection           

self::$soapClient = self::getSoapClient($wsdlMethod);           

self::$soapClient->createConnection(self::SF_SOAPCLIENT_DIR . self::$wsdlMethod . '.wsdl.xml');
           

# Login           

$tmpLogin = self::$soapClient->login(self::SALESFORCE_USER,self::SALESFORCE_PASSself::SALESFORCE_TOKEN);


           # Ensure password is still valid           

if ( $tmpLogin->passwordExpired ) {

                if (self::IN_DEBUG_MODE) {                   

die('Password has expired');               

}               

return false;           

}           

return true;       

} catch ( SoapFault $e ) { 

         $errorMessage = "Error: [{$e->getCode()}] {$e->getMessage()}";           

$errorMessage .= '<br />';           

$errorMessage .= "File:{$e->getFile()}<br />Line: {$e->getLine()}";           

$errorMessage .= '<br />';           

$errorMessage .= '<pre>' . print_r($e->getTrace(), true) . '</pre>';
           

# While in debug we can display the error           

if ( self::IN_DEBUG_MODE ) {               

echo $errorMessage;           

} else {               

 

// To send HTML mail, the Content-type header must be set               

$headers = 'MIME-Version: 1.0' . "\r\n";               

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
               mail(self::ERROR_EMAIL, 'Salesforce Error!', $errorMessage, $headers);           

}           

return false;       

}   

}
        private static function getSoapClient($wsdlMethod) {

       switch (strtolower($wsdlMethod)) {

                

case 'enterprise': 

             

# Require Client               

require_once (self::SF_SOAPCLIENT_DIR . 'SforceEnterpriseClient.php'); 

             

# Set method

               self::$wsdlMethod = 'enterprise';   

 

           # Return object   

        return new SforceEnterpriseClient();   

                break; 

 

         case 'metadata':               

 

# Require Client               

require_once (self::SF_SOAPCLIENT_DIR . 'SforceMetadataClient.php');               

 

# Set method               

self::$wsdlMethod = 'metadata';               

 

# Return object
               return new SforceMetadataClient();               

break;           

 

case 'partner': 

 

                # Require Client               

require_once (self::SF_SOAPCLIENT_DIR . 'SforcePartnerClient.php');               

 

# Set method               

self::$wsdlMethod = 'partner';               

 

# Return object               

return new SforcePartnerClient();               

break;           

 

default:     

 

         die(((self::IN_DEBUG_MODE) ? 'Bad client requested' : 'Unable to connect to salesforce.com'));               break;       

}   

}
    /**     * Logout current connection     */

   

public static function logout() { 

     self::$soapClient->logout();   

}

 

   /**     * Return soap client     */   

 

public static function getClient() {       

return self::$soapClient;   

}

}
ini_set("soap.wsdl_cache_enabled", "0");
?>

 

 

Next i have saved another page as show.php with following code:

 

<?php

 

# Require the connection class

require_once 'sf-lib/SF_Connect.class.php';

# Initialize the connection

if ( !SF_Connect::connect() ) {   

# Connection failed, output error   

die('Unable to connect to salesforce.com');

}

/** * Successful connection * * Process actions here */
# Demo

$query = "SELECT Id, FirstName, LastName from Lead";$queryResult = SF_Connect::getClient()->query($query);$records = $queryResult->records;

foreach ($records as $sObject) {   

echo "Id = ".$sObject->Id; 

echo '<br />';

echo "First Name = ".$sObject->fields->FirstName;

echo '<br />';

       echo "Last Name = ".$sObject->fields->LastName; 

       echo '<hr />';

}

 

# Don't forget to logout

SF_Connect::logout();  
?>

 

 

But above page i.e. show.php is giving error of " Unknown method "Login";

 

Can someone write me a simple code to connect to my salesforce account and fetching custom object records....

Pat PattersonPat Patterson

The only obvious problem I can see is in the call to login - you have

 

$tmpLogin = self::$soapClient->login(self::SALESFORCE_USER,self::SALESFORCE_PASSself::SALESFORCE_TOKEN);

and it should be more like

 

$tmpLogin = self::$soapClient->login(self::SALESFORCE_USER,self::SALESFORCE_PASS . self::SALESFORCE_TOKEN);

Let me know if it's still not working and I'll take a closer look.

 

Cheers,

 

Pat

Vishal Gaddi6Vishal Gaddi6

Fatal error: Class 'SoapClient' not found in C:\wamp\www\sf-lib\toolkit\soapclient\SforceBaseClient.php on line 125Thanx for replying... i have made the changes ...

 

Now i am getting the following error :

 

Fatal error: Class 'SoapClient' not found in C:\wamp\www\sf-lib\toolkit\soapclient\SforceBaseClient.php on line 125

Notice: Use of undefined constant SOAP_SINGLE_ELEMENT_ARRAYS - assumed 'SOAP_SINGLE_ELEMENT_ARRAYS' in C:\wamp\www\sf-lib\toolkit\soapclient\SforceBaseClient.php on line 106



Notice: Use of undefined constant SOAP_SINGLE_ELEMENT_ARRAYS - assumed 'SOAP_SINGLE_ELEMENT_ARRAYS' in C:\wamp\www\sf-lib\toolkit\soapclient\SforceBaseClient.php on line 106

Notice: Use of undefined constant SOAP_COMPRESSION_ACCEPT - assumed 'SOAP_COMPRESSION_ACCEPT' in C:\wamp\www\sf-lib\toolkit\soapclient\SforceBaseClient.php on line 107

Notice: Use of undefined constant SOAP_COMPRESSION_GZIP - assumed 'SOAP_COMPRESSION_GZIP' in C:\wamp\www\sf-lib\toolkit\soapclient\SforceBaseClient.php on line 108

Pat PattersonPat Patterson

It looks like you don't have PHP SOAP installed - if you google for Class 'SoapClient' not found you should find a solution.