2012-03-26 113 views
0

我有一系列Rackspace雲文件存儲的CDN URL,它們引用了一個HTTP地址,我希望將它們轉換爲HTTPS等效。將Rackspace Cloud文件轉換爲HTTP到HTTPS的CDN URL

Rackspace的雲文件CDN網址的格式如下:

http://c186397.r97.cf1.rackcdn.com/CloudFiles Akamai.pdf

而SSL相當於這個網址是:

https://c186397.ssl.cf1.rackcdn.com/CloudFiles Akamai.pdf

到的變化網址是(source):

  1. HTTP變得HTTPS
  2. 第二URI段(在該例子 'R97')變爲 'SSL'

在 'R00' 部分似乎在長度上變化(如一些是 'R6'等等),所以我無法將這些URL轉換爲HTTPS。這是我到目前爲止的代碼:

function rackspace_cloud_http_to_https($url) 
{ 
    //Replace the HTTP part with HTTPS 
    $url = str_replace("http", "https", $url, $count = 1); 

    //Get the position of the .r00 segment 
    $pos = strpos($url, '.r'); 

    if ($pos === FALSE) 
    { 
     //Not present in the URL 
     return FALSE; 
    } 

    //Get the .r00 part to replace 
    $replace = substr($url, $pos, 4); 

    //Replace it with .ssl 
    $url = str_replace($replace, ".ssl", $url, $count = 1); 

    return $url; 
} 

但是,這對於第二個段的長度不同的URL無效。

任何想法讚賞。

回答

1

試試這個:

function rackspace_cloud_http_to_https($url) 
{ 
    $urlparts = explode('.', $url); 

    // check for presence of 'r' segment 
    if (preg_match('/r\d+/', $urlparts[1])) 
    { 
     // replace appropriate segments of url 
     $urlparts[0] = str_replace("http", "https", $urlparts[0]); 
     $urlparts[1] = 'ssl'; 

     // put url back together 
     $url = implode('.', $urlparts); 
     return $url; 
    } 
    else 
    { 
     return false; 
    } 
} 
+0

剛剛經過測試,它適用於不同長度的零件。謝謝。 – 2012-03-26 16:46:01

+0

乾杯,自從我把它寫下來以後,可能會有一些語法錯誤,但是很高興它可以工作! – jessica 2012-03-26 16:48:02

2

我知道這是舊的,但如果你使用這個庫:https://github.com/rackspace/php-opencloud可以使用getPublicUrl()方法的對象上,你只需要使用下面的命名空間

use OpenCloud\ObjectStore\Constants as Constant; 

// Code logic to upload file 
$https_file_url = $response->getPublicUrl(Constant\UrlType::SSL);