user3450650
user3450650

Reputation: 27

Multidimensional array fetch using curl shows invalid argument in foreach

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().

Upvotes: 2

Views: 168

Answers (3)

Leri
Leri

Reputation: 12535

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.

Upvotes: 0

user1978142
user1978142

Reputation: 7948

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

Upvotes: 1

xdazz
xdazz

Reputation: 160833

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);

Upvotes: 0

Related Questions