I was recently handed some of our companies legacy PHP code to work with. There have been updates to the WSDL and API and PHP version (from 5.3 to 5.6) used with this product since the code worked and the code no longer works properly. Specifically;
$response = $mySforceConnection->query($query);
//var_dump($response);
$out='<table><tr><th>Spec Name</th><th>Name</th><th>SF Number</th><th>MW</th></tr>';
foreach ($response->records as $record) {
$out.='<tr><td>'.$record->fields->Spec->fields->Name.'</td>';
$out.='<td>'.$record->fields->Spec->fields->Owner->fields->FirstName.' ';
$out.=$record->fields->Spec->fields->Owner->fields->LastName.'</td>';
$out.='<td>'.$record->fields->Spec->fields->Owner->fields->EmployeeNumber.'</td>';
$out.='</table>';
exit($out);}
Error out with;
[13-Apr-2017 09:42:52 US/Eastern] PHP Notice: Undefined property: stdClass::$fields in T:\sample.php on line 44
[13-Apr-2017 09:42:52 US/Eastern] PHP Notice: Trying to get property of non-object in T:\sample.php on line 44
The $record returned is;
object(stdClass)#8 (3) {
["type"]=>
string(19) "SpecLineItem"
["Id"]=>
array(2) {
[0]=>
string(18) "00k6000000QX8E7AAL"
[1]=>
string(18) "00k6000000QX8E7AAL"
}
["any"]=>
array(1) {
["Spec"]=>
object(stdClass)#9 (3) {
["type"]=>
string(11) "Spec"
["Id"]=>
NULL
["any"]=>
array(2) {
[0]=>
string(74) "<sf:Name>Machineworks - National-Renewal-2015-01-01</sf:Name>"
["Owner"]=>
&object(stdClass)#10 (3) {
["type"]=>
string(4) "User"
["Id"]=>
NULL
["any"]=>
string(119) "<sf:EmployeeNumber>12345</sf:EmployeeNumber><sf:FirstName>John</sf:FirstName><sf:LastName>Doe</sf:LastName>"
}
}
}
}
}
Afraid this falls outside of my very limited PHP scope. How do I adjust the references in the $out construct to properly reference the data? Sorry for something that is likely so basic a question.
Related
I've created a SimpleXMLElement as follows:
$catSearchXml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>'.$categoryContainer);
When I then try to loop round the values using:
foreach ( $catSearchXML->categoryHistogram as $searchCategory )
I get the following error:
PHP Warning: Invalid argument supplied for foreach()
When I do a var_dump($catSearchXml) I get the following:
object(SimpleXMLElement)#18 (1) {
["categoryHistogram"]=> array(3) {
[0]=> object(SimpleXMLElement)#26 (4) {
["categoryId"]=> string(3) "293"
["categoryName"]=> string(14) "Sound & Vision"
["count"]=> string(3) "123"
["childCategoryHistogram"]=> array(8) {
[0]=> object(SimpleXMLElement)#6 (3) {
["categoryId"]=> string(5) "14969"
["categoryName"]=> string(27) "Home Audio & HiFi Separates"
["count"]=> string(2) "89"
}
[1]=> etc...
When I check is_array($catSearchXml->categoryHistogram) it equals false
The following returns nothing: $catSearchXML->categoryHistogram[0]->categoryName
Any thoughts as this is doing my head in???
Thanks in advance.
You're going to kick yourself here, but you've defined $catSearchXml, and you're trying to loop over $catSearchXML. PHP variable names are case-sensitive.
i am trying to get data from:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
Notice: Trying to get property of non-object in **\htdocs\index.php on
line 9 + var_dump() returns: NULL
var_dump($pjs) returns:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
The response is an array.
var_dump($pjs[0]->{'player_name'});
#Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token
i am trying to get data from:
http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson
but if i want to get player_name variable with this code:
<?
$js = file_get_contents('http://api.convoytrucking.net/api.php?api_key=public&show=player&player_name=Mick_Gibson');
$pjs = json_decode($js);
var_dump($pjs->{'player_name'});
?>
i get error:
Notice: Trying to get property of non-object in **\htdocs\index.php on
line 9 + var_dump() returns: NULL
var_dump($pjs) returns:
array(1) { [0]=> object(stdClass)#52 (15) { ["player_name"]=> string(11) "Mick_Gibson" ["player_id"]=> int(88) ["rank"]=> string(12) "FIRE TURTLEE" ["lastseen"]=> int(1393797692) ["registration_date"]=> string(19) "2012-08-10 17:01:34" ["last_mission_date"]=> string(19) "2014-03-02 21:41:50" ["time_offset"]=> int(1) ["house_id"]=> int(611) ["fines"]=> int(0) ["wanted"]=> int(0) ["police_badge"]=> bool(true) ["vip"]=> bool(false) ["staff"]=> NULL ["stats"]=> object(stdClass)#53 (23) { ["score"]=> int(2941) ["convoy_score"]=> int(818) ["ARTIC"]=> int(515) ["DUMPER"]=> int(565) ["TANKER"]=> int(56) ["CEMENT"]=> int(163) ["TRASH"]=> int(7) ["ARMORED"]=> int(9) ["VAN"]=> int(501) ["TOW"]=> int(502) ["COACH"]=> int(4) ["LIMO"]=> int(97) ["ARRESTS"]=> int(272) ["GTA"]=> int(67) ["BURGLAR"]=> int(122) ["HEIST"]=> int(1) ["PLANE"]=> int(48) ["HELI"]=> int(12) ["FAILED"]=> int(312) ["OVERLOADS"]=> int(160) ["TRUCK_LOADS"]=> int(1275) ["ODOMETER"]=> int(28320798) ["TIME"]=> int(2078450) } ["achievements"]=> array(4) { [0]=> string(20) "Professional Trucker" [1]=> string(13) "Gravel Hauler" [2]=> string(12) "Delivery Boy" [3]=> string(7) "Wrecker" } } }
This is because $pjs is an one-element-array of objects, so first you should access the array element, which is an object and then access its attributes.
echo $pjs[0]->player_name;
Actually dump result that you pasted tells it very clearly.
The response is an array.
var_dump($pjs[0]->{'player_name'});
#Balamanigandan your Original Post :- PHP Notice: Trying to get property of non-object error
Your are trying to access the Null Object. From AngularJS your are not passing any Objects instead you are passing the $_GET element. Try by using $_GET['uid'] instead of $objData->token
So in my code I have:
var_dump($poll);
which prints:
object(stdClass)#2 (9) {
["parent"]=> object(stdClass)#3 (3) {
["className"]=> string(5) "Venue"
["__type"]=> string(7) "Pointer"
["objectId"]=> string(10) "HRSCYpe2FK"
}
["no"]=> int(0)
["updatedAt"]=> string(24) "2011-11-06T23:37:17.917Z"
["creator"]=> object(stdClass)#4 (3) {
["className"]=> string(5) "_User"
["__type"]=> string(7) "Pointer"
["objectId"]=> string(10) "96K81tdpM4"
}
["createdAt"]=> string(24) "2011-11-06T23:37:14.591Z"
["yes"]=> int(1)
["objectId"]=> string(10) "U8ly32582W"
["question"]=> string(20) "Negozio conveniente?"
["reports"]=> object(stdClass)#5 (0) { }
}
and then I want to do:
var_dump($poll["parent"]["objectId"]);
but then it gives me a 500 internal server error. Any idea why this is?
UPDATE:
Pulling from the error log it says:
PHP Fatal error: Cannot use object of type stdClass as array
Looks like your $poll variable is a stdclass in which case, you would access the properties like so
var_dump($poll->parent->objectId)
As for your 500 status, at a guess I'd say PHP is configured to trigger this for errors.
It's because you're tying to do a var_dump on an object and not an array.
$poll->parent->objectId;
object :: object :: property
This question already has answers here:
Make var_dump look pretty [duplicate]
(16 answers)
Closed 1 year ago.
I've seen some online pretty print modules for code. Anyone know of one that will format a multi-dimensional array into readable display?
Example, translate this:
array(83) { [0]=> array(2) {
["name"]=> string(11) "CE2 Options"
["type"]=> string(5) "title" } [1]=>
array(1) { ["type"]=> string(4) "open"
} [2]=> array(5) { ["name"]=>
string(8) "Template" ["desc"]=>
string(638) "test description"
["id"]=> string(9) "my_theme"
["type"]=> string(14) "selectTemplate"
["options"]=> array(13) {
Into this...
array(83) {
[0]=> array(2) { ["name"]=> string(11) "My Options" ["type"]=> string(5) "title" }
[1]=> array(1) { ["type"]=> string(4) "open" }
[2]=> array(5) {
["name"]=> string(8) "Template"
["desc"]=> string(638) "Test description"
["id"]=> string(9) "my_theme"
["type"]=> string(14) "selectTemplate"
["options"]=> array(13) {
[0]=> string(10) "test"
If you are dumping it to HTML document use the
<pre></pre>
it does exactly that.
If you want a nicer output than var_dump , then check out the alternatives listed here:
A more pretty/informative Var_dump alternative in PHP?
Particularily http://krumo.sourceforge.net/ provides a much more accessible DHTML view for variable dumps. (It requires an extra include() though.)
And if you actually want to keep the generated output as static html, you might have to write a smallish wrapper script.
the pretty version is just what you get when you have XDebug installed and html_errors is set to On. Then you use var_dump($array). And make sure you set children and depth to what you need. there you go