4

我正在使用以下代码使用 php 连接到 salesforce

 require_once ('SforcePartnerClient.php');
require_once ('SforceHeaderOptions.php');  
require_once ('SforceMetadataClient.php'); 

$mySforceConnection = new SforcePartnerClient(); 
$mySforceConnection->createConnection("cniRegistration.wsdl");
$loginResult = $mySforceConnection->login("username", "password.token");

$queryOptions = new QueryOptions(200);

try {

  $sObject = new stdclass();
  $sObject->Name = 'Smith';
  $sObject->Phone = '510-555-5555';
  $sObject->fieldsToNull = NULL;


  echo "**** Creating the following:\r\n";
  $createResponse = $mySforceConnection->create($sObject, 'Account');

  $ids = array();
  foreach ($createResponse as $createResult) {
    print_r($createResult);
    array_push($ids, $createResult->id);
  }

} catch (Exception $e) {

  echo $e->faultstring;
}

但是上面的代码是连接到salesforce数据库的。但是没有执行创建命令。它给了我以下错误信息

创建以下内容:元素 {}item 在此位置无效

谁能建议我克服上述问题

4

2 回答 2

3

MAK,在您的示例代码中缺少 SessionHeader 和 Endpoint 设置调用

$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);

设置完这些后,如果您仍然看到问题,请检查命名空间 urn

$mySforceConnection->getNamespace

它应该与您的 wsdl中的targetNamespace值匹配

于 2014-06-05T20:32:47.607 回答
0

$mySforceConnection 的值应该指向 partner.wsdl.xml 的 xml 文件。 例如 $SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");

尝试添加下面的代码片段以引用 WSDL。

$sfdc = new SforcePartnerClient();
                // create a connection using the partner wsdl
                $SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");

                $loginResult = false;

                try {
                    // log in with username, password and security token if required
                    $loginResult = $sfdc->login($sfdcUsername, $sfdcPassword.$sfdcToken);
                } 
                catch (Exception $e) {
                    global $errors;
                    $errors = $e->faultstring;
                    echo "Fatal Login Error <b>" . $errors . "</b>";
                    die;
                }           
                // setup the SOAP client modify the headers
                $parsedURL = parse_url($sfdc->getLocation());
                define ("_SFDC_SERVER_", substr($parsedURL['host'],0,strpos($parsedURL['host'], '.')));
                define ("_SALESFORCE_URL_", "https://test.salesforce.com");
                define ("_WS_NAME_", "WebService_WDSL_Name_Here");
                define ("_WS_WSDL_", "soapclient/" . _WS_NAME_ . ".wsdl");
                define ("_WS_ENDPOINT_", 'https://' . _SFDC_SERVER_ . '.salesforce.com/services/wsdl/class/' . _WS_NAME_);
                define ("_WS_NAMESPACE_", 'http://soap.sforce.com/schemas/class/' . _WS_NAME_);

                $urlLink = '';
                try {
                    $client = new SoapClient(_WS_WSDL_);
                    $sforce_header = new SoapHeader(_WS_NAMESPACE_, "SessionHeader", array("sessionId" => $sfdc->getSessionId()));
                    $client->__setSoapHeaders(array($sforce_header));

                } catch ( Exception $e ) {
                    die( 'Error<br/>' . $e->__toString() );
                }

有关错误的更多详细信息,请查看Tech Thought上的链接。

于 2014-02-24T08:25:30.843 回答