-2

Okay, so I have a php variable which stores:

http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback

This is working fine and i want to do the following:

<script type="text/javascript" src="<?php echo $string; ?>"></script>

But it doesn't seem to be working

Thanks for any help

EDIT: Here is my code, tried all 3 answers below but didn't work: http://pastebin.com/xYKW8TTd

user1641732
  • 71
  • 1
  • 2
  • 7

6 Answers6

2

This seems to work as expected:

<?php

    $string="http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback";

?>

<script type="text/javascript" src="<?php echo $string; ?>"></script>

with the output of:

<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback"></script>

Edit: in your source code on pastebin, you seem to have:

$string = "http://gdata.youtube.com/feeds/api/videos/" . $id ."?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback";

which contains &amp; in the place of & which would stop the link working. Was this somethign that pastebin did or was it in your original code?

You can't send HTML codes to the URL window and expect it to work the same way as if it was in a HTML body.

The following code (just edited $id as I am not putting anything in GET and modified & symbols gave:

<html>
<head>
<?php
    //$id = $_GET['id'];
    $id=0;
    $string = "http://gdata.youtube.com/feeds/api/videos/" . $id ."?v=2&alt=json-in-script&callback=youtubeFeedCallback";
?>


<title></title>
</head>
<body>
<?php echo $string; ?><br>
<script type="text/javascript" src="<?php echo $string; ?>"></script>

Had the output of:

<title></title>
</head>
<body>
http://gdata.youtube.com/feeds/api/videos/0?v=2&alt=json-in-script&callback=youtubeFeedCallback<br>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/0?v=2&alt=json-in-script&callback=youtubeFeedCallback"></script>
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

Try this:-

<?php
$str = 'http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback';
?>
<script type="text/javascript" src="<?php echo $str; ?>"></script>
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
0
<?php
$string = 'http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback';
?>

<script type="text/javascript" src="<?=$string;?>"></script>
elo
  • 615
  • 4
  • 11
0

Try this:

echo "<script type=\"text/javascript\" src=\"".$string"\"></script>\n";

If this "does not work", you have some error in string constant which cause javascript error.

Please provide more info, like generated source or exact browser error.

UPDATE:

@user1641732 : As of Mahan's comment. You are including JSON object, not javascript.What you are try to achieve? Did you understand difference between JSON object and javscript code?

rkosegi
  • 14,165
  • 5
  • 50
  • 83
0

Why are you trying to do this? Surly the best way to retrieve data from YouTube would be to do a php cUrl request and decode the json data their, or alternatively, if you really have to you can save the contents to a file with file_put_contents or fopen.

Here is a cUrl example, add your own $url variable:

// get the data via curl
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$rsp = json_decode(curl_exec($ch));
curl_close($ch);
gazzwi86
  • 1,019
  • 1
  • 15
  • 35
0
<?php
$string = "http://gdata.youtube.com/feeds/api/videos/gzDS-Kfd5XQ?v=2&alt=json-in-script&callback=youtubeFeedCallback";
?>

<script type="text/javascript" src="<?php echo $string; ?>"></script>
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
zed Blackbeard
  • 760
  • 12
  • 30