hulk6hulk
hulk6hulk

Reputation: 45

Retrieve api data with curl

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

Upvotes: 0

Views: 155

Answers (1)

Vincenzo Di Gaetano
Vincenzo Di Gaetano

Reputation: 4100

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:

Upvotes: 1

Related Questions