Unable To Show Data in PHP Using ASP.NET Web Service - php

I've created a simple web service in ASP.NET and want that service to be consumed in a PHP application. The web service is as follows:
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<Customer> GetCustomers(int id)
{
id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
List<Customer> lst = null;
using (var context = new DemoEntities())
{
lst = (from c in context.Customer
where c.CustomerID == id
select c).ToList();
}
return lst;
}
In the above web service, a customer id is passed to retrieve customer details. So to consume this service in PHP, I've tried to do the following using nuSoap library as follows and it works almost:
<?php
require_once("lib/nusoap.php"); //Using the nuSoap library
$client = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE); //Passed the ASP.NET web service and an object created
$result = $client->call('GetCustomers', array('id' => 1)); //Called the GetCustomers method and passed a default parameter
foreach($result as $item) //Tried to iterate in a foreach loop
{
echo $item; //Here is the issue - The output returns or returned only the name 'Array'
}
?>
I've done PHP programming a long time ago and trying to figure the issue searching google. I've even tried to access the array index with the web service property directly like the below but seems like missing something or may be not the correct way: Any idea would be appreciated
echo $item[1]->CustName;
echo $item[1]; //Even this
Right now, I am getting the Xml data as follows using the Soap web service:
<ArrayOfCustomer>
<Customer>
<CustomerID>2</CustomerID>
<CustName>John</CustName>
<CustAddress>On Earth</CustAddress>
<CustLocation>On Earth</CustLocation>
<CustSex>Male</CustSex>
<CustType>3</CustType>
<CustStatus>2</CustStatus>
<CustDetails>Great guy - Always regular.</CustDetails>
</Customer>
</ArrayOfCustomer>
Update 1: Used var_dump($item) and currently getting the array elements as follows:
array
'Customer' =>
array
'CustomerID' => string '2' (length=1)
'CustName' => string 'John' (length=7)
'CustAddress' => string 'On Earth' (length=25)
'CustLocation' => string 'On Earth' (length=10)
'CustSex' => string 'Male' (length=4)
'CustType' => string '3' (length=1)
'CustStatus' => string '2' (length=1)
'CustDetails' => string 'Great guy - Always regular.' (length=47)
But when tried with this $item->Customer->CustName, getting this error again - Trying to get property of non-object.
Update 2: Again used var_dump($item) and the result is as follows with PHP programming:
<?php
require_once("lib/nusoap.php");
$client = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE);
$result = $client->call('GetAllCustomers'); //Without any parameter
foreach($result as $item)
{
echo var_dump($item);
}
?>
Output:
array
'Customer' =>
array
0 =>
array
'CustomerID' => string '1' (length=1)
'CustName' => string 'Jack' (length=7)
'CustAddress' => string 'On Earth' (length=25)
'CustLocation' => string 'On Earth' (length=10)
'CustSex' => string 'Male' (length=4)
'CustType' => string '3' (length=1)
'CustStatus' => string '2' (length=1)
'CustDetails' => string 'Regular Customer and always happy to cooperate.' (length=47)
1 =>
array
'CustomerID' => string '2' (length=1)
'CustName' => string 'John' (length=4)
'CustAddress' => string 'On Earth' (length=7)
'CustLocation' => string 'On Earth' (length=10)
'CustSex' => string 'Male' (length=4)
'CustType' => string '3' (length=1)
'CustStatus' => string '2' (length=1)
'CustDetails' => string 'Great guy - Always regular.' (length=25)
Again, tried to use two loops to get the values that's as follows but it only returns first two results and there are total 10 records in the database:
<?php
require_once("lib/nusoap.php");
$client = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE);
$result = $client->call('GetAllCustomers');
$count = count($result);
foreach($result as $item)
{
for($i = 0; $i <= $count; $i++)
{
echo 'Name: ' . $item['Customer'][$i]['CustName'].'<br/>';
}
}
?>
Output:
Name: Jack //Returns only first two records though it should return all the records
Name: John

Simply do a var_dump($item); in php to see how they array structure is.. currently I do not know what the response object is, but for example you can access the keys as such: echo $item->Customer->CustName;
If it is an array response, not an object, then: $item['Customer']['CustName'];

Related

Retrive answer_id in php

Below are my database value and i want to retrieve answer_id using php.
a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}
Try below code, it will return answer id to you.
$str = "a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}";
$arry = unserialize($str);
echo $arry[0]->answer_id;
The Issues is that your serialized String contains back-slashes which would mess with the serialized object. Solution: Remove the backslashes and unserialzed the string and you'd get your object back:
<?php
$strSerializedWithSlashes = 'a:1:{i:0;O:8:\"stdClass\":7:{s:11:\"question_id\";s:1:\"1\";s:13:\"question_text\";s:18:\"This is question 1\";s:9:\"answer_id\";s:1:\"2\";s:11:\"answer_text\";s:4:\"asss\";s:11:\"points_base\";s:1:\"2\";s:6:\"points\";s:1:\"2\";s:15:\"custom_response\";s:0:\"\";}}';
$strSerializedWithoutSlashes = str_replace("\\", "", $strSerializedWithSlashes);
$objUnSerialized = unserialize($strSerializedWithoutSlashes);
var_dump($objUnSerialized);
// DUMPS::
array (size=1)
0 =>
object(stdClass)[1]
public 'question_id' => string '1' (length=1)
public 'question_text' => string 'This is question 1' (length=18)
public 'answer_id' => string '2' (length=1)
public 'answer_text' => string 'asss' (length=4)
public 'points_base' => string '2' (length=1)
public 'points' => string '2' (length=1)
public 'custom_response' => string '' (length=0)
You can test it here: https://eval.in/571535
And now; to get you answer_id You can simply do this:
<?php
$objData = $objUnSerialized[0];
$answerID = $objData->answer_id;
var_dump($answerID); // DUMPS: '2'

SQL/PHP sort fields from a form to make insert

I've got a form which users can add new rows by clicking on a button and automatically, it adds +1 on the field's name.
So for example, I've got my train_id_1, train_type_1 and my user wants to add a new one, so now I've got train_id_2 and train_type_2.
In order to save this in my database, I would like to sort and seperate train_type_1 / train_type_2... to make a foreach and then to save in my database.
So, the var_dump of my $_POST looks like :
array (size=60)
'train_id_1' => string ' 07:36' (length=6)
'train_type_1' => string ' -Z' (length=3)
'user_id_1' => string 'CPN' (length=3)
'event_criter_1' =>
array (size=3)
0 => string 'test' (length=4)
1 => string '234' (length=3)
2 => string '532' (length=3)
'train_id_2' => string ' 08:32' (length=6)
'train_type_2' => string ' -X' (length=3)
'user_id_2' => string 'CPN' (length=3)
'event_criter_2' =>
array (size=3)
0 => string 'TESTG' (length=5)
1 => string 'GGG' (length=3)
2 => string 'AETG' (length=4)
'train_id_3' => string ' 08:36' (length=6)
'train_type_3' => string ' -Z' (length=3)
'user_id_3' => string 'CPN' (length=3)
'event_criter_3' =>
array (size=1)
0 => string '' (length=0)
'train_id_4' => string ' 09:04' (length=6)
'train_type_4' => string ' -X' (length=3)
'user_id_4' => string 'CPN' (length=3)
'event_criter_4' =>
array (size=1)
0 => string '' (length=0)
Do you know how I can make abcd_1 separate from abcd_2 to make my foreach (or another solution) ?
Thank you!
Loop through your array, matching _1 and insert the posted elements as one row. Then increment and match _2 etc..
$postedElements = $_POST;
$elementsPerRow = 4;
$numRows = count($postedElements)/$elementsPerRow;
// loop through the number of 'rows' to insert
for($i=1;$i<=$numRows;$i++){
// Build an array to store matched elements to insert
$elementsToInsert = array();
//process the complete _POST array each time...
foreach($postedElements as $name => $value){
// ...get the 'row' (the bit after the underscore)...
//list($value,$row) = explode('_',$name); // doesn't work for 2 underscores..
$row = end(explode($name)); // This will
if($row == $i){
// ...and add elements that match to an insert array
// $elementsToInsert[] = $name;
$elementsToInsert[$name] = $value;
}
}
// insert $elementsToInsert into DB
}
I think, at first You need to have a amount of records in array through count($arr). Then use usual for loop:
//output of field names
for ($i = 0; $i < count($arr); $i++){
//Work with array
}
Make a new array.
Then add a data from loop into new array.
I think it could help You.
And by which reason You need to sort them?

make serialized form POST to php

my problem is that I'm sending html form data via jquery here's the code :
$('#search_button').click(function(event) {
var track_load = 0; //total loaded record group(s)
var loading = false; //to prevents multipal ajax loads
var total_groups = <?php echo $total_groups; ?>; //total record group(s)
var Data = $("#srch_form").serializeArray();
console.log(Data);
$('#results').load("autoload_process.php", {
data:Data,
'group_no':track_load},
function() {track_load++;}); //load first group
});
and in the php receiving page I'm getting an array with one array per name&value pair inside like this :
array (size=4)
0 =>
array (size=2)
'name' => string 'srch_word' (length=9)
'value' => string '' (length=0)
1 =>
array (size=2)
'name' => string 'srch_cat-2' (length=10)
'value' => string '2' (length=1)
2 =>
array (size=2)
'name' => string 'srch_cat-5' (length=10)
'value' => string '5' (length=1)
3 =>
array (size=2)
'name' => string 'srch_cat-6' (length=10)
'value' => string '6' (length=1)
But before with html only form post I was getting one array with the pairs inside :
array (size=4)
'srch_word' => string '' (length=0)
'srch_cat-2' => string '2' (length=1)
'srch_cat-5' => string '5' (length=1)
'srch_cat-6' => string '6' (length=1)
And I use to access the values like an associative array would do ($_POST['srch_word'], or even just $srch_word would work ), now I can't do that.
¿How can I get one array from the jquery post or how can I reduce this array of arrays to one array in the php file or what should I do in this case?
I would appreciate your help.
this is sample maybe usefull for you as long number of child array always two
<?php
$keys = array();
$value = array();
$serialize = array(array('name' => 'srch_word', 'value' => ''), array('name' => 'srch_cat-2', 'value' => '2'));
foreach ($serialize as $key) {
array_push($keys, $key['name']);
array_push($value, $key['value']);
}
$final = array_combine($keys, $value);
echo '<pre>',print_r($final),'</pre>';
?>
replace $serialize as your array define
please vote if you like it

Slow php function, Putting a lot of data in to array

I have a function that get data from an SQL Server database. The result set is around 5900 records with two columns (Code and Name) in each record.
However this function can take quite a while to run 2-5 or even longer in some cases.
Here is the code:
public function GetAllProductCodes()
{
$Connection = Database::odbc("sage");
$sql = "SELECT Code, Name FROM STKStockItemView";
$ResultSet = Database::execute($Connection, $sql);
$i = 1;
$array = array();
while ($row = odbc_fetch_array($ResultSet)) {
foreach ($row AS $key => $value) {
$array[$i][$key] = $row[$key];
}
$i++;
}
return $array;
}
What would be a more optimum way of running this? Here is a dump of the output array:
array (size=5867)
1 =>
array (size=2)
'Code' => string '0010' (length=4)
'Name' => string 'Product name' (length=14)
2 =>
array (size=2)
'Code' => string '0957' (length=4)
'Name' => string 'Product name' (length=27)
3 =>
array (size=2)
'Code' => string '0958' (length=4)
'Name' => string 'Product name' (length=20)
4 =>
array (size=2)
'Code' => string '1050' (length=4)
'Name' => string 'Product name' (length=16)
I believe the problem lies in your database wrapper. Also, are you making a new database connection every time on your function?
$Connection = Database::odbc("sage");
because if so, that's not a good idea. You should make the database connection once, in another file (say config.php).
Another thing to keep under consideration is to implement a cache system to deal with large amount of data, that way you won't have to query the database as much. If you're not familiar with cache, a simple cache class to use would be "phpfastcache".

multiarray - how to keep first 3 index groups of each source type

I have a multi-array where I need to keep the first 3 index groups and remove the rest from the multiarray (in each group).
See multiarray here: https://gist.github.com/no1uknow/6887497
So:
In this example I need the multi-array to keep: The first 3 Heavy, Lite, Intermediate, etc (these are identified by the source_type_cd)
Example of the Lite part of the array after the first 3 are kept:
0 =>
array (size=9)
'validated_ata' => string '25' (length=2)
'source_type_cd' => string 'Lite' (length=4)
'validated_subata' => string '22' (length=2)
'action_cd' => string '3' (length=1)
'object_cd' => string '5' (length=1)
'malfunction_cd' => string '29' (length=2)
'corrective_action_txt' => string 'Repair-Passenger Seat-Loose / Displaced' (length=39)
'rec_count' => string '00050' (length=5)
'group_id' => int 48
1 =>
array (size=9)
'validated_ata' => string '25' (length=2)
'source_type_cd' => string 'Lite' (length=4)
'validated_subata' => string '22' (length=2)
'action_cd' => string '3' (length=1)
'object_cd' => string '5' (length=1)
'malfunction_cd' => string '1' (length=1)
'corrective_action_txt' => string 'Repair-Passenger Seat-Inoperative' (length=33)
'rec_count' => string '00047' (length=5)
'group_id' => int 44
2 =>
array (size=9)
'validated_ata' => string '25' (length=2)
'source_type_cd' => string 'Lite' (length=4)
'validated_subata' => string '22' (length=2)
'action_cd' => string '3' (length=1)
'object_cd' => string '5' (length=1)
'malfunction_cd' => string '31' (length=2)
'corrective_action_txt' => string 'Repair-Passenger Seat-Worn / Chaffed / Frayed' (length=45)
'rec_count' => string '00042' (length=5)
'group_id' => int 50
You will simply have to loop through the array and look if it has any of those values and place into a new array.
Example (where $arr is your multiarray):
// My silly solution for knowing what to look for
// When one is found, it will be removed from the array.
$find = array('Lite','Lite','Lite','Intermediate','Intermediate','Intermediate','Heavy','Heavy','Heavy');
// New array where your the values you want will be placed in
$new_arr = array();
foreach($arr as $v) {
// No need to keep looking if there's no more to find.
if(empty($find))
break;
// Look in $find array if current "source_type_cd" is still sought-after
$key = array_search($v['source_type_cd'], $find);
if($key !== false) {
$new_arr[] = $v; // Add to new array
unset($find[$key]); // Remove from "find" array
}
}
Thanks Colandus I actually figured it out like this... I was trying to avoid to many loops.
In a loop above this I set the source_type_cd in an array:
$groups[]=$value['source_type_cd']
Next I loop through the array and take the top 3 of each group by using array_splice twice and then merging back into a new array.
(also, I'm using a start and end point ($group_count).)
$start = 0;
$group_count = $i+1;
$top_count = 3;
foreach($groups as $k => $v) {
$top_count_array = array_merge((array)$top_count_array, (array)array_slice(array_slice($sorted_array, $start, $group_count, true),0,$top_count,true));
$start = $start+$group_count;
}
var_dump($top_count_array);
Again appreciate the input. Tried to shorten this code down from so many loops. Also requirements will change for grabbing the amount of $top_count and $group_count... Needed something a little more dynamic. :-)

Categories