I use woocommerce rest api with php library
https://github.com/woothemes/wc-api-php
I with this document create a api for send product
https://woocommerce.github.io/woocommerce-rest-api-docs/#create-a-product
In wordpress i use plugin “upload to ftp” for upload image in other server , for this plugin when i set image in data my api return ‘error syntax’ and product cant creat.
this api problem with plugin “upload to ftp” and icant fix it.
full error :
<br />
<b>Fatal error</b>: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Syntax error in C:\xampp\htdocs\apiwo\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php:375
Stack trace:
#0 C:\xampp\htdocs\apiwo\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php(414): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#1 C:\xampp\htdocs\apiwo\vendor\automattic\woocommerce\src\WooCommerce\Client.php(56): Automattic\WooCommerce\HttpClient\HttpClient->request('products', 'POST', Array)
#2 C:\xampp\htdocs\apiwo\send.php(56): Automattic\WooCommerce\Client->post('products', Array)
#3 {main}
thrown in
<b>C:\xampp\htdocs\apiwo\vendor\automattic\woocommerce\src\WooCommerce\HttpClient\HttpClient.php</b> on line
<b>375</b>
<br />
my php code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
'http://teset.com',
'ck_xxxxxxxxxxxxxxxxxxxxxxxxx',
'cx_xxxxxxxxxxxxxxxxxxxxxxxxx',
[
'debug' => true,
'ssl_verify' => false,
// 'return_as_array' => false,
'timeout' => 120,
'wp_api' => true,
'version' => 'wc/v2',
]
);
if(isset($_POST['RESULT_TextField-1'])&&isset($_POST['RESULT_TextArea-2'])&&isset($_POST['RESULT_TextArea-3'])&&isset($_POST['RESULT_TextArea-4'])&&isset($_POST['RESULT_TextField-5'])&&isset($_FILES['RESULT_FileUpload-6'])){
$error['status'] ="success";
$check = getimagesize($_FILES["RESULT_FileUpload-6"]["tmp_name"]);
if($check !== false) {
// echo json_encode($_FILES["RESULT_FileUpload-6"]);
$array = explode(',',$_POST['RESULT_TextArea-4']);
$i = 0;
foreach ($array as $v) {
$tag[$i]['name']=$v;
$tag[$i]['slug']=str_replace(" ","_",$v);
$i=$i+1;
}
$target_dir = "upload/";
$Server = "http://test.com/dataip/";
$target_file = $target_dir . basename($_FILES["RESULT_FileUpload-6"]["name"]);
if (move_uploaded_file($_FILES["RESULT_FileUpload-6"]["tmp_name"], $target_file)) {
//images
$images[0]['src']=$Server.$target_file;
$images[0]['alt']=$_FILES["RESULT_FileUpload-6"]["name"];
$images[0]['name']=$_FILES["RESULT_FileUpload-6"]["name"];
$images[0]['position']=0;
//Meta data
$meta_data[0]['key']="mweb_single_video_embed";
$meta_data[0]['value']=$_POST['RESULT_TextField-5'];
//data
$data['name'] = $_POST['RESULT_TextField-1'];
$data['type'] = "simple";
$data['status'] = "pending";
//$data['price'] = "21.99";
//$data['regular_price'] = "21.99";
$data['description'] = $_POST['RESULT_TextArea-2'];
$data['short_description'] = $_POST['RESULT_TextArea-3'];
$data['meta_data'] = $meta_data;
$data['tags'] = $tag;
$data['images'] = $images;
try {
// Array of response results.
$results = $woocommerce->post('products', $data);
// Last request data.
$lastRequest = $woocommerce->http->getRequest();
$lastRequest->getUrl(); // Requested URL (string).
$lastRequest->getMethod(); // Request method (string).
$lastRequest->getParameters(); // Request parameters (array).
$lastRequest->getHeaders(); // Request headers (array).
$lastRequest->getBody(); // Request body (JSON).
// Last response data.
$lastResponse = $woocommerce->http->getResponse();
$lastResponse->getCode(); // Response code (int).
$lastResponse->getHeaders(); // Response headers (array).
$lastResponse->getBody(); // Response body (JSON).
$error['data'] =$results;
} catch (HttpClientException $e) {
$error['status'] ="error";
$error['message'] = $e->getMessage();
//$error['request'] =$e->getRequest();
//$error['response'] =$e->getResponse();
}
unlink($target_file);
} else {
$error['status'] ="error";
$error['message'] ="تصویر نامعتبر می باشد";
}
} else {
$error['status'] ="error";
$error['message'] ="تصویر نامعتبر می باشد";
}
}else{
$error['status'] ="error";
$error['message'] ="اطلاعات ارسالی ناقص می باشد";
}
print_r(json_encode($error));
?>
HttpClient.php Line 375 :
protected function processResponse()
{
$body = $this->response->getBody();
if (0 === strpos(bin2hex($body), 'efbbbf')) {
$body = substr($body, 3);
}
$parsedResponse = \json_decode($body);
// Test if return a valid JSON.
if (JSON_ERROR_NONE !== json_last_error()) {
$message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Invalid JSON returned';
Line 375=> throw new HttpClientException($message, $this->response->getCode(), $this->request, $this->response); // Line 375
}
$this->lookForErrors($parsedResponse);
return $parsedResponse;
}
“upload to ftp ” => https://richer.tw/upload-to-ftp