Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Woocommerce Order API using Guzzle

I can able to get the Order Details using guzzle. But i can't able to update the Order.

Here is my code:

use stdClass;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;


$data = new stdClass();
$data->fulfillment = new stdClass();

$trackingUrl = "123456789";

$shopUrl = "localhost/Test";
$consumerKey = "cs_mykey";
$consumerSecret = "ck_mykey";
$orderId = "123";

$subPath = "/wc-api/v2/orders/".$orderId;

$data->fulfillment->tracking_url = $trackingUrl;
$data->fulfillment->status = 'completed';

$headers = array(
   'Content-Type: application/json'
);

$method = "POST";

$url = "http://localhost/Test/wc-api/v2/orders/123?oauth_consumer_key=ck_mykey&consumer_key=ck_mykey&consumer_secret=cs_mykey&oauth_timestamp=1505544895&oauth_nonce=9ecd49e80860e09ddaf91f148451532620976b8d&oauth_signature_method=HMAC-SHA256&oauth_signature=mysignature";

$Result = callApi($url, json_encode($data), $headers, $method);

echo '<pre>'; print_r($Result);


function callApi($url = NULL, $body = NULL, $headers = array(), $requestType = "POST")
{
   $client = new GuzzleHttp\Client();
   $body = $body ? $body : new stdClass();
   $request = $client->POST($url)->setPostField($body)->send();

  $data = $request->getBody()->getContents();
  return json_decode($data);

}

Using the above code i will get the error as below

resulted in a 400 Bad Request response: {"errors":[{"code":"woocommerce_api_missing_callback_param","message":"Missing parameter data"}]} ' in C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Middleware.php(65): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\promises\src\Promise.php(203): in C:\xampp\htdocs\Guzzle\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113

I don't know what i missed in above. Help me to sort out.

Thanks.

like image 538
rAm Avatar asked Sep 16 '17 07:09

rAm


1 Answers

After i changed the below function now order updated.

function callApiPost($url = NULL, $body = NULL, $headers = array(), $requestType = "POST")
{

    $client = new Client();
    $body = $body ? $body : new stdClass();
    $request = new Request($requestType, $url, $headers, json_encode($body));
    $response = $client->send($request, ['timeout' => 10]);
    if($requestType === 'DELETE') {
        return $httpCode = $response->getStatusCode();
    } else {
        $data = $response->getBody()->getContents();
        return json_decode($data);
    }
}

By using guzzle we don't need to use Post function, we just get the request and send the request it will update the order.

a small change..

like image 200
rAm Avatar answered Sep 22 '22 16:09

rAm