[英]Converting Rackspace Cloud Files CDN URLs from HTTP to HTTPS
I have a series of Rackspace Cloud Files CDN URLs stored which reference an HTTP address and I would like to convert them to the HTTPS equivalent. 我存储了一系列Rackspace Cloud Files CDN URL,这些URL引用一个HTTP地址,我想将它们转换为等效的HTTPS。
Rackspace Cloud Files CDN URLs are in the following format: Rackspace Cloud Files CDN URL的格式如下:
http://c186397.r97.cf1.rackcdn.com/CloudFiles Akamai.pdf http://c186397.r97.cf1.rackcdn.com/CloudFiles Akamai.pdf
And the SSL equivalent for this URL would be: 此URL的SSL等效项为:
https://c186397.ssl.cf1.rackcdn.com/CloudFiles Akamai.pdf https://c186397.ssl.cf1.rackcdn.com/CloudFiles Akamai.pdf
The changes to the URL are ( source ): URL的更改为( source ):
The 'r00' part seems to vary in length (as some are 'r6' etc.) so I'm having trouble converting these URLs to HTTPS. “ r00”部分的长度似乎有所不同(有些是“ r6”等),因此我很难将这些URL转换为HTTPS。 Here's the code I have so far:
这是我到目前为止的代码:
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;
}
This however does not work for URLs where the second segment is of a different length. 但是,这不适用于第二段长度不同的URL。
Any thoughts appreciated. 任何想法表示赞赏。
I know this is old, but if you are using this library: https://github.com/rackspace/php-opencloud you can use the getPublicUrl() method on the object, you just need to use the following namespace 我知道这很旧,但是如果您使用的是此库: 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);
Try this: 尝试这个:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.