skip to Main 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.
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


  1. 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:

    $id = /*some id*/;
    $data = file_get_contents('http://localhost/api-woocommerce/single_product_connect.php?edit='.$id);
    
    Login or Signup to reply.
  2. 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. You can check this by just adding this json_last_error() after json_decode() like this

    $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

    $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.

    With curl, it’s very easy

    // 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search