PHP get data from web services that returns an stdClass Object
I’m doing like this: database.php UserManager class : And in any file i want to use getUsers(): Solution 1: StdClass is PHP generic object (as said in the comments). Desired Outcome: => To have all the items in an array so it can be encoded into a JSON string.
PHP get data from web services that returns an stdClass Object
I am trying to get the values from my web service using the following code:
$client = new SoapClient($url); $response = $client->GetInfoByNumeroContrato(array( 'schema' => $schema, 'numContrato' => $numContract ));
I have a method called GetInfoByNumeroContrato that receives two parameters. When I call my web service the $response gives me a stdClass Object as shown below:
stdClass Object ( [GetInfoByNumeroContratoResult] => stdClass Object ( [schema] => [any] => 000559500trueTITULAR BNMARIA DORES PEIXOTO220125424DORESRIBEIRO50@GMAIL.COMR DAMIAO DE GOIS ENTRADA 71 BL 1 1 ANDAR N 90000003194460-2921646013411950-10-07T00:00:00+01:00Divorciado003501600006968540097PT50003501600006968540097CGDIPTPLBilhete de IdentidadeSENHORA DA HORAFeminino4010003002015-09-16T00:00:00+01:0024.90falseSENIORtrue2015-10-05T00:00:00+01:00true2016-09-05T00:00:00+01:002015-11-05T00:00:00+00:00M0PT47111799DÉBITO DIRETOCONNECTA000559500falseAGREGADOJOSE LUIS RIBEIRO FAIEL220125424R DAMIAO DE GOIS ENTRADA 71 BL 1 1 ANDAR N 90000003204460-2921646013411983-08-23T00:00:00+01:00Outro003501600006968540097Bilhete de IdentidadeSENHORA DA HORAMasculino4010003012015-09-16T00:00:00+01:0024.90falseSENIORtrue2015-10-05T00:00:00+01:00true2016-09-05T00:00:00+01:002015-11-05T00:00:00+00:00M0PT47111799DÉBITO DIRETOCONNECTA ) [_exception] => )
The idea is to get each value individually from [any] and the POST each one. For example get «000559500» then «true» and so on.
I have already tried to parse this but without success.
Is there any better way to go about this?
print $response->GetInfoByNumeroContratoResult->any;
I get the following answer:
000559500trueTITULAR BNMARIA DORES PEIXOTO220125424DORESRIBEIRO50@GMAIL.COMR DAMIAO DE GOIS ENTRADA 71 BL 1 1 ANDAR N 90000003194460-2921646013411950-10-07T00:00:00+01:00Divorciado003501600006968540097PT50003501600006968540097CGDIPTPLBilhete de IdentidadeSENHORA DA HORAFeminino4010003002015-09-16T00:00:00+01:0024.90falseSENIORtrue2015-10-05T00:00:00+01:00true2016-09-05T00:00:00+01:002015-11-05T00:00:00+00:00M0PT47111799DÉBITO DIRETOCONNECTA000559500falseAGREGADOJOSE LUIS RIBEIRO FAIEL220125424R DAMIAO DE GOIS ENTRADA 71 BL 1 1 ANDAR N 90000003204460-2921646013411983-08-23T00:00:00+01:00Outro003501600006968540097Bilhete de IdentidadeSENHORA DA HORAMasculino4010003012015-09-16T00:00:00+01:0024.90falseSENIORtrue2015-10-05T00:00:00+01:00true2016-09-05T00:00:00+01:002015-11-05T00:00:00+00:00M0PT47111799DÉBITO DIRETOCONNECTA
Is it possible to get one by one?
Is it possible to receive from the web service insted getting every thing in [any] recive for example: [contractNumer] => value then [Name] => value and so on? Individualy?
I know this is a late answer, but might save hours for someone else:
$xml= simplexml_load_string($response->GetInfoByNumeroContratoResult->any); print_r($xml); //an array containing the whole XML print $x->contractNumber; //individually
Php — codeigniter extract stdClass Object, Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives
StdClass when fetch as OBJ by default
I prefer use $obj->value than $array[‘value’], so i used
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
The problem is when i do a simple req :
public function getUsers() < return $this->conn->query('SELECT * FROM users')->fetch(); >
It return me a stdClass and i can’t get users list. (Or i don’t know how, but it look weird, I never saw stdClass before).
So with FETCH_OBJ attribute I’ve got:
$list = $users->getUsers(); var_dump($list); // object(stdClass)#4 (6)[. ]
Without FETCH_OBJ attribute I’ve got: (With same code)
And I have 2 rows 6 cols so this line give me error: (not error, but not what I want)
$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
Do you know why ? Is there another way to set FETCH_OBJ as default ?
I just wanna make fetch() instead _fetch(pdo::fetch_obj)_.
Edit: I don’t own database class. I’m doing like this: database.php
try < $database = new \PDO('mysql:host=localhost;dbname=projet', 'root', ''); $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); > catch(PDOExeption $e)
class UserManager < private $conn; public function __construct(\PDO $database) < $this->conn = $database; > public function getUsers() < return $this->conn->query('SELECT * FROM users')->fetch(); > >
And in any file i want to use getUsers():
$users = new UserManager($database); $list = $users->getUsers();
StdClass is PHP generic object (as said in the comments).
I’ll try to answer to other questions you ask in comments too.
You’re only getting one result because you are using fetch which give the next result , false when there isn’t any result left. You have a few options for that :
- Yield Using a generator. It means that you «pause» the method, and you have to call it like an Iterable. For example :
public function getUsers() < yield $this->conn->query('SELECT * FROM users')->fetch(); >
foreach ($userManager->getUsers() as $user) < // $user is a row of your database >
Be carefull with this because it can let your database connection open.
- FetchAll It returns all the results of your query at once in an array. Note that if you have only one result, you will have an array of one element. For this one you would do :
public function getUsers() < return $this->conn->query('SELECT * FROM users')->fetchAll(); > //. $users = $userManager->getUsers(); foreach ($users as $user) < // your code >
Also not that if you want an instance of a User class you’ve made, you can use PDO::FETCH_CLASS . You will find the documentation here.
Small edit in case it’s not quite clear, you can specify the format as ::fetch() argument such as :
public function getUsers() < return $this->conn->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC); // will return results as associative array even if you specified FETCH_OBJ in your constructor >
You can use setFetchMode in the query object, like this $query->setFetchMode(PDO::FETCH_OBJ); And then just $users = $query->fetch() . More in the documentation.
Php — fetch data from an object, Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build your employer brand ; Advertising Reach developers & technologists worldwide; About the company
PDO fetchAll() gives me stdClass
I am learning about PHP blobs and I got an issue with the data I am getting back.
Notice: Undefined property: stdClass::$
I am getting this for my results when I echo the column names in the foreach loop
$sql = "SELECT * FROM tbl_blobs"; $stmt = $db->prepare($sql); $stmt->execute(); $stmt->bindColumn(1, $name); $stmt->bindColumn(2, $mime); $stmt->bindColumn(3, $data, PDO::PARAM_LOB); $arr = []; $items = $stmt->fetchAll(PDO::FETCH_OBJ); foreach ($items as $item) < echo $item->name; >
Is there a function I need to use to wrap the results in or am I missing a step?
Desired Outcome: => To have all the items in an array so it can be encoded into a JSON string. One of the items is a blob.
Output of array using var_dump($items);
array(5) < [0]=>object(stdClass)#3 (4) < ["ID"]=>string(1) «1» [«NAME»]=> string(7) «test123» [«IMAGE»]=> string(17861) (followed by a blob)
You could use PDO::FETCH_ASSOC instead of PDO:FETCH_OBJ it would directly return an associative array.
See fetchAll arguments for more options
Set it for the whole PDO instance
If you want to have this behavior for all your queries in your PDO connection, you can set it with:
$db = new \PDO('mysql:host='.$dbhost.';dbname='.$dbname, $dbusername, $dbpassword, [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]);
Another way it to set it after the PDO instanciation, with:
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
edit: added global alternatives
Arrays — PHP : Notice: Undefined property: stdClass, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more
Fetching data from moodle database
I am trying to query some data from the moodle database. I am using the data manipulation API. Here is my code. When I run it on the browser, i get a blank screen. i reall dont know what the $enrolids is. Is it an associative array or what. Even when I try create an associative array, I still get a blank screen.
get_records_sql('SELECT enrolid FROM WHERE userid=?', array($userid)); echo $enrolids['enrolid']; ?>
I figured it out: To anyone with the same problem. Here is the solution. Moodle is object oriented. Meaning, the queries made, return an Array of stdclass objects i.e:
Array ( [1] => stdClass Object ( [id] => 1 [status] => 0 [enrolid] => 5 [userid] => 3 [timestart] => 0 [timeend] => 2147483647 [modifierid] => 0 [timecreated] => 0 [timemodified] => 0 ) [2] => stdClass Object ( [id] => 2 [status] => 0 [enrolid] => 6 [userid] => 3 [timestart] => 0 [timeend] => 2147483647 [modifierid] => 0 [timecreated] => 0 [timemodified] => 0 ) )
Use the -> notation to fetch whatever value you desire i.e If the above array is stored in a variable e.g. $arr, do the following to access status for example
Php — Convert/cast an stdClass object to another class, I’m using a third party storage system that only returns me stdClass objects no matter what I feed in for some obscure reason. So I’m curious to know if there is a way to cast/convert an stdClass object …