[英]array becomes null because I can't send product id with get_file_content
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. 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它在一个数组中。 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
主要问题是我无法使用 get_file_content 将产品 ID 发送到 single_product_connect.php,因此它显示 $ 数据数组为空
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; single_product.php 代码;
<?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; single_product_connect.php 代码;
<?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:\xampp\htdocs\api-woocommerce\single_product.php on line 19////The code in line 19:
警告:在第 19 行为 foreach() 提供的参数无效////第 19 行的代码:
<?php foreach ( $data as $row ) : ?>
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.当您使用 file_get_contents 获取 single_product_connect.php 和 http 时,如果将查询参数连接到 Z572D4E421E5E6B9BC111D815E8Z07,则可以将“edit”作为参数传递。
Try this change:试试这个改变:
$id = /*some id*/;
$data = file_get_contents('http://localhost/api-woocommerce/single_product_connect.php?edit='.$id);
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 on json_decode()
becomes null
.正如您所提到
can't send product id to single_product_connect.php with get_file_content
因此,当您调用远程 url 时,返回值是一个空字符串,在json_decode()
上变为null
You can check this by just adding this json_last_error()
after json_decode()
like this您可以通过像这样在
json_decode()
json_last_error()
来检查这一点
$data = json_decode($data, true);
$erorr = json_last_error();
var_dump($data);
echo $error;
You can first append the product_id to the url and then can make the call to the url using file_get_contents
like this您可以先将 append 的 product_id 传递给 url,然后可以像这样使用
file_get_contents
调用 url
$url = "http://localhost/api-woocommerce/single_product_connect.php?product_id=<product_id>";
$data = json_decode(file_get_contents($url), true);
but I would suggest you to either use curl
( recommended ) or fsockopen
.但我建议您使用
curl
(推荐)或fsockopen
。
With curl, it's very easy使用 curl,非常简单
// create curl resource
$ch = curl_init();
// set url
$url = "http://localhost/api-woocommerce/single_product_connect.php?product_id=<product_id>";
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$data = json_decode($output, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo json_last_error_msg();
exit();
}
foreach ($data as $row) {
// do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.