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
Boulder MikeBoulder Mike 

[PHP5] Retrieving query results from PHP5

I'm using PHP 5.0.4 and have been able to query objects, but haven't yet been able to retrieve results from the object getting passed back from Salesforce. I think the problem is that PHP's SOAP tools don't correctly assign the returned object as the specific type of object, but instead everything is assigned the generic sObject type.

For example:
// Using PHP5's SOAP tools to access SFDC and reassign the endpoint and header.
// $SESSION and $ENDPOINT are passed-in from the custom link in Salesforce.
$wsdl = "file://E:/webroot/sfdc/enterprise.wsdl.xml";
$client = new SoapClient( $wsdl , array('trace' => 1) ) ;
$client->__setLocation($ENDPOINT);
$sessheader = new SoapHeader('urn:enterprise.soap.sforce.com', 'SessionHeader',
(object) array('sessionId'=>$SESSION), 0);

// Lookup the user's id from SFDC
$user = array('type'=>'user'); // In this case, this doesn't matter
$result = $client->__soapCall('getUserInfo', array((object) $user) , NULL,
$sessheader, $ohdrs);
print "User ID: ".$result->result->userId . "
";
$userId = $result->result->userId ;

// Lookup user's address, phone, etc.
$query = "Select Id, Username, LastName, FirstName, Street, City, State, PostalCode, Email, Phone FROM User WHERE Id = '$userId'";
$query_array = array('queryString' => $query );
$result2 = $client->__soapCall('query', array((object) $query_array) , NULL,
$sessheader, $other_headers );

// You can see from the getLastResponse that the result is getting returned
echo "
Last Response:
";
echo $client->__getLastResponse();
echo "

";

// Access the records returned
$records = $result2->result->records;


At this point, even though I can access the records object, I can't access the fields returned. I think it may be possible to use Soap_Vars to force the returned object to be a User object instead of an sObject, but I'm reaching.

Anyone gotten this far yet using PHP5? Any clue hot to proceed?

Michael
Boulder MikeBoulder Mike
Now I'm pretty certain that the problem is that PHP5 doesn't process the WSDL to create classes for each "type". In the SOAP response, the records returned from the query are of type "sObject", not "User" (which is the type of records I'm looking up).

So the challenge remains: how to "convert" this "sObject" to a User object so that I can access the fields that are so clearly visible in the SOAP response. I think this may be possible using SoapVar, but I'm just at a loss on how to accomplish that.

Here is the SOAP response, just for reference's sake:

[?xml version="1.0" encoding="UTF-8"?]
[soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"]
[soapenv:Body]
[queryResponse xmlns="urn:enterprise.soap.sforce.com"]
[result]
[done]true[/done]
[queryLocator xsi:nil="true"/]

[records xsi:type="sf:sObject" xmlns:sf="urn:sobject.partner.soap.sforce.com"]
[sf:type]User[/sf:type]
[sf:Id]00530000000xxxxx[/sf:Id]
[sf:Id]00530000000xxxxx[/sf:Id]
[sf:Username]xxxx[/sf:Username]
[sf:LastName]xx[/sf:LastName]
[sf:FirstName]xx[/sf:FirstName]
[sf:Street]xx[/sf:Street]
[sf:City]xxx[/sf:City]
[sf:State]xx[/sf:State]
[sf:\PostalCode]xxxxx[/sf:\PostalCode]
[sf:Email]xxxxxxxxxxxxxxx@xxxxxxxxxx[/sf:Email]
[sf:\Phone]xxx[/sf:\Phone]
[/records]

[size]1[/size]
[/result]
[/queryResponse]
[/soapenv:Body]
[/soapenv:Envelope]

Message Edited by Boulder Mike on 07-19-2005 04:13 PM

Message Edited by Boulder Mike on 07-19-2005 04:14 PM

SuperfellSuperfell
That response is from the Partner API, which isn't strongly typed, it sounds like what you're looking for is the enterprise WSDL/API.
Boulder MikeBoulder Mike
Simon, you were exactly right. I made the mistake of referring to the Partner API in the web link instead of the Enterprise API. That solved the problem and I can now access values from the query result.

For the benefit of others using PHP5, I'm enjoying some success integrating with SForce. I'm using PHP 5.0.4 with PHP's built-in SOAP module, so the SalesForceClient toolkit is not necessary.

Here's some sample PHP code I use from a web link (that happens to be on an Account). I pass the session, (Enterprise API) endpoint and username in the link.

** Take the space out of the initial PHP tag: < ?php should not have a space. HTML tags get whacked by this forum unfortunately.


< ?php
// QUERY THE USER TABLE IN SALESFORCE

// Establish a connection back to SFDC
$SESSION = $_GET['session'];
$ENDPOINT = $_GET['endpoint'];
$USERNAME = $_GET['username'];

// +----------------------------------------------------------------------+
// | ACCESS SALESFORCE.COM USING THE SUPPLIED SESSION AND ENDPOINT
// +----------------------------------------------------------------------+
$wsdl = "file://E:/webroot/sfdc/enterprise.wsdl.xml";
// The trace paramenter, below, enables examination of the SOAP messages
// using __getLastRequest and __getLastResponse
$client = new SoapClient( $wsdl , array('trace' => 1) ) ;
$client->__setLocation($ENDPOINT);
$sessheader = new SoapHeader('urn:enterprise.soap.sforce.com', 'SessionHeader',
(object) array('sessionId'=>$SESSION), 0);


// Lookup the userid
$user = array('type'=>'user'); // In this case, this doesn't matter because getUserInfo doesn't take any paramenters, but the __soapCall requires an array.
// In the next line, use __soapCall so you can pass the session header variable
$getUserInfoResult = $client->__soapCall('getUserInfo', array((object) $user) , NULL,
$sessheader, $ohdrs);
$userId = $getUserInfoResult -> result -> userId ;
// echo "userid: $userId";


// Lookup user's address, phone, etc.
$query = "Select Id, Username, LastName, FirstName, Street, City, State, PostalCode, Email, Phone FROM User WHERE Id = '$userId'";
// In the next line, we establish queryString as the parameter to pass, according
// to the wsdl. You can easily see the functions using $client->__getFunctions()
$query_array = array( 'queryString' => $query );
$queryResult = $client->__soapCall('query', array((object) $query_array) , NULL,
$sessheader, $other_headers );

// Access the records contained in the result.
$records = $queryResult->result->records;

// Since the result has exactly one record, I don't need to
// iterate through multiple records and can access the values directly.
echo "LastName: ".$records->LastName."
";
echo "Street: ".$records->Street."
";

?>