Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php RESTAPI实现示例代码_Php_Json_Rest - Fatal编程技术网

Php RESTAPI实现示例代码

Php RESTAPI实现示例代码,php,json,rest,Php,Json,Rest,我对实现API一无所知。我确实懂一点PHP。在这种情况下,我需要调用RESTAPI方法来清除CDN服务器上的缓存。有人能帮我一些示例代码吗 以下是请求示例: PUT <<url>> Authorization: TOK:12345-12345 Accept: application/json Content-Type: application/json Host: api.edgecast.com Content-Length: 87 { "MediaPath":"

我对实现API一无所知。我确实懂一点PHP。在这种情况下,我需要调用RESTAPI方法来清除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"
}
PUT
授权:TOK:12345-12345
接受:application/json
内容类型:application/json
主持人:api.edgecast.com
内容长度:87
{
“MediaPath”:”
“MediaType”:“3”
}
有人能帮我编写代码来实现这个RESTAPI请求吗?
提前感谢。

懒得从头开始写,所以从谷歌在搜索结果第一页建议的网站上抄袭

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

附:源代码搜索请求:

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

对于任何想使用EdgeCast API的人来说,这是我完全实现的Grunt任务的源代码要点。在我的示例中,您会发现我使用一个节点模块来执行curl命令,该命令清除CDN


这就是我花了几个小时试图让HTTP请求在节点内工作后得到的结果。我能够得到一个用Ruby和Python工作的,但没有满足这个项目的要求。

天哪,它是令人惊讶的粉红色。还有一个奇怪的仙女+1.
//## 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.
  }
}