我存储了一系列Rackspace云文件CDN URL,这些URL引用了一个HTTP地址,我想将它们转换为HTTPS等效地址。
Rackspace云文件CDN URL的格式如下:
http://c186397.r97.cf1.rackcdn.com/CloudFilesAkamai.pdf
这个URL的SSL等价物是:
https://c186397.ssl.cf1.rackcdn.com/CloudFilesAkamai.pdf
URL的更改是(来源):
- HTTP变为HTTPS
- 第二个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不起作用。
任何想法都值得赞赏。
我知道这是旧的,但如果您使用这个库: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);
试试这个:
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;
}
}