0

I am writing woocommerce rest api. When I connect to single_product_connect.php with curl, I get "snytax error" error. how can i solve the problem.My aim is to write this code. Select any of the products in my Woocommerce product list and view the details of that product. 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;

<?php

$ch = curl_init();


$url = "http://localhost/api-woocommerce/single_product_connect.php?<queryParam>=<product_id>";
curl_setopt($ch, CURLOPT_URL, $url);


curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


$output = curl_exec($ch);


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) {
    $row['name'];
}
?>

single_product_connect.php

<?php
require __DIR__ . '/vendor/autoload.php';

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
'https://blog.test.com',
'ck_bb7cb132da9dfac541570',
'cs_327a1926654e4e20610',
[
    'wp_api' => true,
    'version' => 'wc/v3',
    'query_string_auth' => true // Force Basic Authentication as query string true and using under 
HTTPS
]
);
?>
<?php $product_id = $_GET['edit'];?>
<?php echo json_encode($woocommerce->get("products/{$product_id}",$data)); ?>

error screen;

Syntax error

1 Answer 1

1

The error you are getting most likely relates to the single_product_connect.php file:

Parse error: syntax error, unexpected 'HTTPS' (T_STRING), expecting ']' in your code on line 14

All you need is:

  • Add a comma after 'query_string_auth' => true (so it will be 'query_string_auth' => true,)
  • Remove the HTTPS text if it is not to be there (keep in mind that written in this way it is considered as a constant, if you want to use it as a string you will have to use quotes)

To go beyond your question, I recommend that you also read these answers:

Sign up to request clarification or add additional context in comments.

2 Comments

I'll try it. If you remember I had a problem with woocommerce. It still goes on and I wanted to experiment with curl this time. My main problem is that the array fails when I send the id with get.
I tried but the same problem persists.:stackoverflow.com/questions/67660295/… You solved my previous problem and now I will be very grateful if you can. I have been dealing with this error for days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.