Curl PHP运行代码 [英] Curl PHP Run code

查看:132
本文介绍了Curl PHP运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从wocoommerce rest API得到这个代码,它说,一个产品可以从curl添加,所以经过大量的研究,我已经启用了我的PHP中的Curl,并测试了一个代码,说curl在我的机器上启用但我有零线索如何运行下面的API代码

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测试代码:

<?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找到将产品上传到商店

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


推荐答案

p>您可以使用以下代码运行curl

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

这篇关于Curl PHP运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆