2011-03-21 54 views
1

我對實現API一無所知。我確實知道PHP。我有一種情況需要調用REST API方法來清除CDN服務器上的緩存。有人可以幫助我一些示例代碼?REST API實現示例代碼

下面是示例請求:

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的請求? 在此先感謝。

回答

1

懶得寫從amazingly pink谷歌建議在結果的第一頁的網站從頭開始複製。

$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

+0

我的上帝,它是驚人的粉紅色。還有一個奇怪的仙女。 +1 – 2011-03-21 06:52:54

2

我必須找到硬盤的方式了。這已經過測試(與我原來的代碼稍加修改)

//## 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. 
    } 
} 
0

Here是我的源依據爲我全面實施咕嚕任務爲別人思考與EdgeCast API工作。在我的示例中,您會發現我使用節點模塊來執行清除CDN的curl命令。

這就是我花了數小時試圖獲得一個HTTP請求在Node中工作後結束了。我能夠使用Ruby和Python工作,但沒有達到此項目的要求。