0

I got this below code from a wocoommerce rest API where it says that a product can be added from curl so after lot of research i have enabled the Curl in my PHP and tested a code which says the curl is enabled on my Machine But i have zero clue how to run the below API code in

Curl Test code:

<?php

    // Script to test if the CURL extension is installed on this server

    // Define function to test
    function _is_curl_installed() {
        if  (in_array  ('curl', get_loaded_extensions())) {
            return true;
        }
        else {
            return false;
        }
    }

    // Ouput text to user based on test
    if (_is_curl_installed()) {
        echo "cURL is <span style=\"color:blue\">installed</span> on this server";
        } else {
        echo "cURL is NOT <span style=\"color:red\">installed</span> on this server";
    }
?>

Api Found that uploads the Products to the store

curl -X POST https://example.com/wc-api/v3/products \
    -u consumer_key:consumer_secret \
    -H "Content-Type: application/json" \
    -d '{
  "product": {
    "title": "Premium Quality",
    "type": "simple",
    "regular_price": "21.99",
    "description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.",
    "short_description": "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.",
    "categories": [
      9,
      14
    ],
    "images": [
      {
        "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-front.jpg",
        "position": 0
      },
      {
        "src": "http://example.com/wp-content/uploads/2015/01/premium-quality-back.jpg",
        "position": 1
      }
    ]
  }
}'

2 Answers2

1

you can run curl by using the following code

/* Initiate a CURL resource */
$resCurl = curl_init();


/* If you want to send a JSON Request, use these options */
curl_setopt( $resCurl, CURLOPT_HTTPHEADER,  array( 'Content-type: APPLICATION/JSON; CHARSET=UTF-8' ) );
curl_setopt( $resCurl, CURLOPT_POSTFIELDS, $jreq );


curl_setopt( $resCurl, CURLOPT_POST, true );
curl_setopt( $resCurl, CURLOPT_URL, "http://www.example.com");
curl_setopt( $resCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $resCurl, CURLOPT_SSL_VERIFYPEER, false);

 if( curl_exec( $resCurl ) === false ) {
     echo 'Curl error: ' . curl_error( $resCurl );
     curl_close( $resCurl );
 } else {

 $result = curl_exec( $resCurl );
curl_close( $resCurl );

echo $result;
}
stackers
  • 385
  • 4
  • 18
  • Thanks for the reply but i have a quick question Where should i add the products Code and the ck and sk Number –  Sep 02 '15 at 08:58
0

Firstly you'll need to create a PHP array that can be encoded to that JSON string

$aData = array(
  'product' => array(
      'title' => 'Premium Quality',
      'type' => 'simple'
      ...............................
  )
);

Then use json_encode to convert it to JSON

$sData = json_encode($aData);

Then use the following PHP code

$ch = curl_init('https://example.com/wc-api/v3/products');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $sData);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'consumer_key:consumer_secret');                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($sData))                                                                       
); 
$result = curl_exec($ch);
  • First of all thanks for the reply but i have a quick question where should i add consumer_secret,consumer_key –  Sep 01 '15 at 11:12
  • I've edited my answer. you need to use curl_setopt($ch, CURLOPT_USERPWD, 'username:password'); – Michael Westcott Sep 01 '15 at 11:25
  • when loaded that gives me error as follows `{"errors":[{"code":"woocommerce_api_authentication_error","message":"oauth_consumer_key parameter is missing"}]}` –  Sep 02 '15 at 08:06
  • http://woothemes.github.io/woocommerce-rest-api-docs/#authentication. According to that you can't use HTTP Basic auth for HTTPS calls. Bit of googling would help. – Michael Westcott Oct 07 '15 at 02:56