Curl PHP Run code

2019-09-12 17:56发布

问题:

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
      }
    ]
  }
}'

回答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;
}


回答2:

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);