简体   繁体   English

数组变为 null 因为我无法使用 get_file_content 发送产品 ID

[英]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.

相关问题 get_file_content不适用于https - get_file_content not working for https get_file_content()耗尽了允许的内存大小(读取.dat文件) - get_file_content() exhausted allowed memory size(reading .dat file) 如何使用mysql行结果作为get_file_content变量包含文件名? - How to use mysql row result as a get_file_content variable incclude filename? 无法获取 POST 数据,因为它始终是 null - Can't get POST data because it's always null 通过ID获取mysql数组的乘积 - Get product of mysql array by ID 如何在外部文件中获取VirtueMart 2产品的产品数据? - How can I get product data of a VirtueMart 2 product in an external file? 为什么我无法从按钮获取ID值以与我的Ajax jQuery发送 - Why i can't get id value from button to send with my ajax jquery 我在存储在变量中的文件中有一个JSON对象,但由于它嵌套在数组中,因此似乎无法对其执行任何操作 - I have a JSON object in a file that I stored in a variable, but I can't seem to do anything I want with it because it is nested inside of an array 如何获取产品的 ID,以便我可以在 Bootstrap 模式中显示每个产品 - How do i get the ID of a product so i can display each product in Bootstrap Modal 如何匹配xml中的产品ID属性,然后将产品元素放入数组中 - How can I match product ID attribute from my xml and then put the product elements into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM