2

I have an array like this which I fetch using file_get_content and here is other side url (http://sample.com/change.php)code from where I fetch array.

$a=array();
$a=Db::getInstance()->ExecuteS("SELECT *
FROM tablename
LIMIT 0 , 2
;");

$a=(array)$a;
print_r($a);

Then i use

$result = file_get_contents('http://sample.com/change.php');

That is the output of $result:

Array
(
    [0] => Array
        (
            [id_stock_available] => 1
            [id_product] => 1
            [id_product_attribute] => 0
            [id_shop] => 1
            [id_shop_group] => 0
            [quantity] => 3
            [depends_on_stock] => 0
            [out_of_stock] => 2
        )

    [1] => Array
        (
            [id_stock_available] => 2
            [id_product] => 2
            [id_product_attribute] => 0
            [id_shop] => 1
            [id_shop_group] => 0
            [quantity] => 1
            [depends_on_stock] => 0
            [out_of_stock] => 2
        )

)

When I apply foreach for $result:

foreach ($result as $value) {
    var_dump($value);
    //var_dump($value['installed'];
}

it shows me Invalid argument supplied for foreach().

  • try to check `var_dump($result)` first – user1978142 Apr 29 '14 at 08:28
  • It shows me output string(614) "Array ( [0] => Array ( [id_stock_available] => 1 [id_product] => 1 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 3 [depends_on_stock] => 0 [out_of_stock] => 2 ) [1] => Array ( [id_stock_available] => 2 [id_product] => 2 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 1 [depends_on_stock] => 0 [out_of_stock] => 2 ) ) " – user3450650 Apr 29 '14 at 08:44
  • have your tried this? $result = (array) $result; then do a var_dump again – Gian Carlo Apr 29 '14 at 08:56
  • 1
    if you look carefully, `$result` is actually a string. it says on your `var_dump` – user1978142 Apr 29 '14 at 09:01
  • @GianCarlo it shows me array(1) { [0]=> string(614) "Array ( [0] => Array ( [id_stock_available] => 1 [id_product] => 1 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 3 [depends_on_stock] => 0 [out_of_stock] => 2 ) [1] => Array ( [id_stock_available] => 2 [id_product] => 2 [id_product_attribute] => 0 [id_shop] => 1 [id_shop_group] => 0 [quantity] => 1 [depends_on_stock] => 0 [out_of_stock] => 2 ) ) " } now foreach invaid argument not showing but it is also showing correct array in foreach loop like am using foreach ($result as $csv) {} – user3450650 Apr 29 '14 at 09:10
  • @kevinabelita ok Please let me know how we can convert it as an array – user3450650 Apr 29 '14 at 09:11
  • check your curl if it is returning an array and not a string. – Gian Carlo Apr 29 '14 at 09:15
  • Am using file_get_contents now and from url file am using $a=array(); $a=Db::getInstance()->ExecuteS("SELECT * FROM `ps_stock_available` LIMIT 0 , 2 ;"); $a=(array)$a; print_r($a); Plz lemme know anything wrong here? – user3450650 Apr 29 '14 at 09:18
  • its hard to guess whats on the other side of the code, try to post other relevant codes to be examined. – user1978142 Apr 29 '14 at 09:22
  • $a=array(); $a=Db::getInstance()->ExecuteS("SELECT * FROM `ps_stock_available` LIMIT 0 , 2 ;"); $a=(array)$a; print_r($a); it is other side code and I have tried also file_get_contents now but same thing happening... – user3450650 Apr 29 '14 at 09:25
  • i mean add it on the main question, not here on the comments section, properly construct it again with your code from the start – user1978142 Apr 29 '14 at 09:26

3 Answers3

1

On your php file you must change it to this:

// your query here
$a = Db::getInstance()->ExecuteS("SELECT * FROM tablename LIMIT 0 , 2;");
// then output it as JSON
header('Content-Type: application/json');
echo json_encode($a);

Then to get it on the other php:

$result = file_get_contents('http://sample.com/change.php');
$values = json_decode($result, true);

The values should be on $values as an array

user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Your welcome and no problem, so a tip: next time try to add and post relevant code, because this might be very helpful in solving the problem. – user1978142 Apr 29 '14 at 09:45
  • @user3450650 Also read-up on why singleton is bad practice: http://stackoverflow.com/questions/4595964/is-there-a-use-case-for-singletons-with-database-access-in-php/4596323#4596323 – Leri Apr 29 '14 at 09:58
0

file_get_contents returns the content of response (return false on fail), which is a string, but not an array.

You have to parse the result of print_r back to array, which is not a good idea.

So use some encode/decode strategy to do this:

echo json_encode($a);

and

$result = file_get_contents('http://sample.com/change.php');
$result = json_decode($result, true);
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

print_r is not a proper way for serializing data for Web Services because serializing data means that it should be deserialized on the receiving end. As you know there's not a function that deserializes data outputted by print_r i.e. takes a string created by print_r and returning array that was passed in. Also from the docs:

print_r — Prints human-readable information about a variable

Human-readable doesn't mean computer-readable as well.

You have 2 alternatives:

  1. php's serialize/unserialize:

    // web service.
    $a=(array)$a;
    serialize($a);
    
    // receiving end.
    $result = file_get_contents('http://sample.com/change.php');
    var_dump(unserialize($result);
    
  2. json_encode/json_decode:

    // web service.
    $a=(array)$a;
    json_encode($a);
    
    // receiving end.
    $result = file_get_contents('http://sample.com/change.php');
    var_dump(json_decode($result);
    

I recommend using json because almost every platform can be client of your web service. While with native serializing, consumer of your WS will be client written in php also.

Leri
  • 12,367
  • 7
  • 43
  • 60
  • @user3450650 You're welcome. You need to mark answer which helped you with a tick on the left hand-side of answer. – Leri Apr 29 '14 at 09:43