开发者

Correct syntax for php inside a feed request

I have a very basic query string which passes a ID to a receiving page.

On that page, I need to dynamically call the YouTube API, giving my playlistID.

I'm having to use PHP for this, and it's a little out of my comfort zone, so hopefully someone can wade in with a quick fix for me.

Here开发者_Python百科 is my variable

$playlist;

And I need to replace the 77DC230FBBCE4D58 below with that variable.

$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/77DC230FBBCE4D58?v=2';

Any help, as always, greatly appreciated!


Once the $playlist variable is set you can construct the feed URL as :

$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';

or

$feedURL = "http://gdata.youtube.com/feeds/api/playlists/$playlist?v=2";


$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/'.rawurlencode($playlist).'?v=2';

Or perhaps a little neater:

$feedurl = sprintf(
    'http://gdata.youtube.com/feeds/api/playlists/%s?v=2',
    rawurlencode($playlist)
);

(Note: rawurlencode is used just in case [not that it's likely with YouTube playlist IDs] the $playlist value contains any funky characters.)

More infos:

  • String concatenation with the . operator
  • Encoding potentially "unsafe" URL characters with rawurlencode
  • Adding values to "formatted strings" with sprintf


$feedURL = 'http://gdata.youtube.com/feeds/api/playlists/' . $playlist . '?v=2';

you use a full stop to join strings in PHP

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜