If I look at your error message:
You have uncommitted work pending. Please commit or rollback before calling out
this means that you make a (rest) webservice call, after you started a database transaction, either you do your webservice call before doing DML statements, or you do your webservice call in an @future method.
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";
Best Answer
I have managed to figure out a solution to this.
I was using the wrong encoding, after finding this link - http://www.salesforce.com/developer/docs/api/Content/implementation_considerations.htm
And reading the sections around Internationalization and Character Sets and XML Compliance it become apparent that the string (body) that I was uploading was already in UTF-8, and just required Base64 Encoding.
Below is my working code - just in case you are experiencing issues too.