REST API 实现示例代码

发布于 2024-10-24 20:09:38 字数 390 浏览 2 评论 0原文

我对实现 API 一无所知。我确实了解一点 PHP。我遇到一种情况,需要调用 REST API 方法来清除 CDN 服务器上的缓存。有人可以帮我一些示例代码吗?

以下是示例请求:

PUT <<url>>
Authorization: TOK:12345-12345
Accept: application/json
Content-Type: application/json
Host: api.edgecast.com
Content-Length: 87
{
   "MediaPath":"<<urlhere>>"
   "MediaType":"3"
}

有人可以帮我编写代码来实现此 REST API 请求吗? 提前致谢。

I know nothing about implementing an API. I do know PHP a bit. I have a situation where I need to call a REST API method to purge cache on a CDN server. Can somebody help me with some sample code?

The following is the sample request:

PUT <<url>>
Authorization: TOK:12345-12345
Accept: application/json
Content-Type: application/json
Host: api.edgecast.com
Content-Length: 87
{
   "MediaPath":"<<urlhere>>"
   "MediaType":"3"
}

Can somebody help me with code to implement this rest api request?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

jJeQQOZ5 2024-10-31 20:09:38

我也必须找到困难的方法。这已经过测试(对我的原始代码稍作修改)

//## GoGrid PHP REST API Call

define('TOKEN','XXXXX-XXXXX-XXXXX-XXXXXX');     // found on the cdn admin My Settings
define('ACCOUNT_NUMBER','XXXX');        // found on the cdn admin Top Right corner

function purgeCacheFileFromCDN($urlToPurge) {
  //## Build the request
  $request_params = (object) array('MediaPath' =>  $urlToPurge, 'MediaType' => 8);   // MediaType 8=small 3=large
  $data = json_encode($request_params);

  //## setup the connection and call.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.edgecast.com/v2/mcc/customers/'.ACCOUNT_NUMBER.'/edge/purge');
  curl_setopt($ch, CURLOPT_PORT , 443);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLINFO_HEADER_OUT, 1);                  // For debugging
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);             // no caching  
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);            // no caching  
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: tok:'.TOKEN, 'Content-Type: application/json','Accept: application/json', 'Content-length: '.strlen($data)));
  $head = curl_exec($ch);
  $httpCode = curl_getinfo($ch);
  curl_close($ch);

  //## check if error
  if ($httpCode['http_code'] != 200) {
    echo 'Error reported: '.print_r(array($head,$httpCode),1);  // output it to stdout this will be emailed to me via cron capture.
  }
}

I had to find the hard way too. This has been tested (with slight modifications from my original code)

//## GoGrid PHP REST API Call

define('TOKEN','XXXXX-XXXXX-XXXXX-XXXXXX');     // found on the cdn admin My Settings
define('ACCOUNT_NUMBER','XXXX');        // found on the cdn admin Top Right corner

function purgeCacheFileFromCDN($urlToPurge) {
  //## Build the request
  $request_params = (object) array('MediaPath' =>  $urlToPurge, 'MediaType' => 8);   // MediaType 8=small 3=large
  $data = json_encode($request_params);

  //## setup the connection and call.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://api.edgecast.com/v2/mcc/customers/'.ACCOUNT_NUMBER.'/edge/purge');
  curl_setopt($ch, CURLOPT_PORT , 443);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLINFO_HEADER_OUT, 1);                  // For debugging
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);             // no caching  
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);            // no caching  
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: tok:'.TOKEN, 'Content-Type: application/json','Accept: application/json', 'Content-length: '.strlen($data)));
  $head = curl_exec($ch);
  $httpCode = curl_getinfo($ch);
  curl_close($ch);

  //## check if error
  if ($httpCode['http_code'] != 200) {
    echo 'Error reported: '.print_r(array($head,$httpCode),1);  // output it to stdout this will be emailed to me via cron capture.
  }
}
吾家有女初长成 2024-10-31 20:09:38

懒得从头开始写,所以从 令人惊叹的粉红色网站,谷歌在结果的第一页建议。

    $data = array("a" => $a);
    $ch = curl_init($this->_serviceUrl . $id);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

    $response = curl_exec($ch);
    if(!$response) {
        return false;
    }

PS:源搜索请求:http://www.google。 ru/search?q=php+sample+put+request+curl

Was too lazy to write from the scratch so copied from amazingly pink site that Google advises in the first page of results.

    $data = array("a" => $a);
    $ch = curl_init($this->_serviceUrl . $id);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

    $response = curl_exec($ch);
    if(!$response) {
        return false;
    }

PS: The source search request: http://www.google.ru/search?q=php+sample+put+request+curl

究竟谁懂我的在乎 2024-10-31 20:09:38

这里是我完全实现的 Grunt 任务的源要点,供其他人思考关于使用 EdgeCast API。您会在我的示例中发现,我使用节点模块来执行清除 CDN 的curl 命令。

这是我花了几个小时尝试让 HTTP 请求在 Node.js 中工作后得到的结果。我能够让一个在 Ruby 和 Python 中工作,但没有满足这个项目的要求。

Here is my source gist for my fully implemented Grunt task for anyone else thinking about working with the EdgeCast API. You'll find in my example that I use a node module to execute the curl command which purges the CDN.

This was that I ended up with after spending hours trying to get an HTTP request to work within Node. I was able to get one working in Ruby and Python, but did not meet the requirements of this project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文