简体   繁体   English

卷曲PHP运行代码

[英]Curl PHP Run code

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 我从wocoommerce rest API那里获得了下面的代码,其中说可以从curl中添加产品,因此经过大量研究,我在PHP中启用了Curl并测试了一个代码,该代码表明在我的机器上启用了curl但是我有零提示如何在下面运行以下API代码

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 Api发现将产品上传到商店

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

you can run curl by using the following code 您可以使用以下代码运行curl

/* 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;
}

Firstly you'll need to create a PHP array that can be encoded to that JSON string 首先,您需要创建一个可以编码为该JSON字符串的PHP数组

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

Then use json_encode to convert it to JSON 然后使用json_encode将其转换为JSON

$sData = json_encode($aData);

Then use the following PHP code 然后使用以下PHP代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM