0

HTTPアドレスを参照する一連のRackspaceCloudFiles CDN URLを保存しており、それらを同等のHTTPSに変換したいと思います。

Rackspace Cloud FilesCDNURLは次の形式です。

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

また、このURLに相当するSSLは次のようになります。

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

URLへの変更は次のとおりです(ソース):

  1. HTTPはHTTPSになります
  2. 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;
}

ただし、これは、2番目のセグメントの長さが異なるURLでは機能しません。

どんな考えでもありがたいです。

4

2 に答える 2

3

これは古いことは知っていますが、このライブラリを使用している場合: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);
于 2014-04-09T15:04:34.007 に答える
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;
    }
}
于 2012-03-26T16:41:35.370 に答える