I cannot see the details of the product I have selected in my product list with the woocommerce rest api
The main problem is that when I write the id of the product, I see it as json, but I have a problem when I want to include it in an array.
The main problem is that I can’t send product id to single_product_connect.php with get_file_content, so it shows $ data array empty
form code;
<form action="single_product.php" name="update" method="get">
<td><input type="submit" name="edit"id="edit" value="<?= $row['id']; ?>"/></td></form>
single_product.php code;
<?php
$data = file_get_contents('http://localhost/api-woocommerce/single_product_connect.php');
$data = json_decode($data, true);
?>
<?php foreach ( $data as $row ) : ?>
<?= $row['id']; ?>
<?= $row['name']; ?> //I wrote a small piece for testing
<?php endforeach; ?>
single_product_connect.php code;
<?php $product_id = $_GET['edit'];?>
<?php echo json_encode($woocommerce->get("products/{$product_id}",$data)); ?>
error screen;
Warning: Invalid argument supplied for foreach() in C:xampphtdocsapi-woocommercesingle_product.php on line 19////The code in line 19:
<?php foreach ( $data as $row ) : ?>
2
Answers
When you use file_get_contents to get single_product_connect.php with http you can pass "edit" as a parameter if you concatenate query parameters to your url.
Try this change:
As you’ve mentioned
can't send product id to single_product_connect.php with get_file_content
so, when you’re calling the remote url, the return value is an empty string which onjson_decode()
becomesnull
. You can check this by just adding thisjson_last_error()
afterjson_decode()
like thisYou can first append the product_id to the url and then can make the call to the url using
file_get_contents
like thisbut I would suggest you to either use
curl
(recommended) orfsockopen
.With curl, it’s very easy