PHP:从Instagram API图像URL剥离协议


PHP: Stripping protocol from Instagram API image urls

这是一个来自Instagram JSON:的片段

"images":{
    "standard_resolution":
        { "url":"http:'/'/scontent-a.cdninstagram.com'/hphotos-xfa1'/t51.2885-15'/10593467_370803786404217_1595289732_n.jpg","width":640,"height":640}
}

我想从['images']['standard_resolution']['url']中删除协议。我尝试过:

.parse_url($value['images']['standard_resolution']['url'], PHP_URL_HOST) . parse_url($value['images']['standard_resolution']['url'], PHP_URL_PATH).

但什么也不做!我认为这与JSON中的/转义(http:'/'/)有关。因为如果我尝试

.parse_url("http://www.google.com", PHP_URL_HOST) . parse_url("http://www.google.com", PHP_URL_PATH).

它工作得很好。我想保持轻松。。并且不要使用regex。CCD_ 4将是完美的。

为什么您甚至需要执行替换或regex?

如果使用json_decode,则转义斜杠将取消转义。

这样做:

<?php
$foo = '{"images":{"standard_resolution":{"url":"http:'/'/scontent-a.cdninstagram.com'/hphotos-xfa1'/t51.2885-15'/10593467_370803786404217_1595289732_n.jpg","width":640,"height":640}}}';
$bar = json_decode($foo, true);
$baz = 
parse_url($bar['images']['standard_resolution']['url'], PHP_URL_HOST) . 
parse_url($bar['images']['standard_resolution']['url'], PHP_URL_PATH);
echo $baz;

将输出:

scontent-a.cdninstagram.com/hphotos-xfa1/t51.2885-15/10593467_370803786404217_1595289732_n.jpg

参见:

http://ideone.com/F0c4m9