[SalesForce] POST to SOAP Salesforce API in PHP

I am implementing my own SOAP API with Salesforce. I am not using the Salesforce PHP Client add-on.

I am trying to create a new object, such as a Contact or Lead, via the API.

My code to POST the sessionId and new Contact information.

$soapVar =  array('parameters'=>array('sessionId' => 'sessionId', 'Contact' => array('Company' => 'Test', 'LastName' => 'Last Name')));
$client = new SoapClient("wsdl file", null);
$header = new SoapHeader("[serverUrl]", "AuthHeader", $soapVar, false)l
$client->__setLocation("[serverUrl]");
$result = __soapCall("create, $soapVar");
print_r($result;)

I get the following error: Uncaught SoapFault exception: [soapenv:Client] No operation available for request {urn:enterprise.soap.sforce.com}create

Where am I supposed to be posting to and what am I doing wrong?

When I login through SOAP to request a sessionId and the location, I get the sessionId and the serverUrl is https://xx.salesforce.com/services/Soap/m/27.0/xxxxx

I am using the Sandbox mode.

Thanks!

Best Answer

Not sure why you're not using the PHP Toolkit, but here's some PHP that creates a Contact.

First, with the Enterprise SOAP API

// Namespaces
$ns1 = "urn:sobject.enterprise.soap.sforce.com";
$ns2 = "urn:enterprise.soap.sforce.com";

$client = new SoapClient($enterpriseWsdlFile, array ( 
    'trace' => TRUE, // Useful for debugging!
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS // Preserve your sanity
));

$header = new SoapHeader($ns2, "SessionHeader", array ('sessionId' => $sessionId));

// $enterpriseUrl has form https://na1.salesforce.com/services/Soap/c/33.0/[ORG_ID]
$client->__setLocation($enterpriseUrl);
$client->__setSoapHeaders($header);

$contact = new stdclass();
$contact->FirstName = 'Jane';
$contact->LastName = 'Doe';

// You can send multiple records in one call by just putting more
// entries in this array
$sObjects = array( 
    new SoapVar($contact, SOAP_ENC_OBJECT, "Contact", $ns1)
);

try {
    $result = $client->create(new SoapParam($sObjects, 'sObjects'));
    echo "Created records\n";
    foreach ($result->result as $value) {
        echo $value->id."\n";
    }

    // Debug
    echo "SOAP Client returned: \n";
    print_r($result);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Debug...
echo "SOAP Request: \n";
print_r($client->__getLastRequest());
echo "\n";
echo "SOAP Response: \n";
print_r($client->__getLastResponse());
echo "\n";

And the same thing, with the Partner SOAP API:

// Namespaces
$ns1 = "urn:sobject.partner.soap.sforce.com";
$ns2 = "urn:partner.soap.sforce.com";

$client = new SoapClient($partnerWsdlFile, array ( 
    'trace' => TRUE, // Useful for debugging!
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS // Preserve your sanity
));

// $partnerUrl has form https://na1.salesforce.com/services/Soap/u/33.0/[ORG_ID]
$client->__setLocation($partnerUrl);

$header = new SoapHeader($ns2, "SessionHeader", array ('sessionId' => $sessionId));
$client->__setSoapHeaders($header);

$fields = array (
    new SoapVar('Contact', XSD_STRING, null, null, 'type', $ns1),
    new SoapVar('Jane', XSD_STRING, null, null, 'FirstName'),
    new SoapVar('Doe', XSD_STRING, null, null, 'LastName')
);

// You can send multiple records in one call by just putting more
// entries in this array
$sObjects = array( 
    new SoapVar($fields, SOAP_ENC_OBJECT, null, null) 
);

try {
    $result = $client->create(new SoapParam($sObjects, 'sObjects'));
    echo "Created records\n";
    foreach ($result->result as $value) {
        echo $value->id."\n";
    }

    // Debug
    echo "SOAP Client returned: \n";
    print_r($result);
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Debug...
echo "SOAP Request: \n";
print_r($client->__getLastRequest());
echo "\n";
echo "SOAP Response: \n";
print_r($client->__getLastResponse());
echo "\n";
Related Topic