-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

2
  • 3
    can you define not working? is it printing the link in source? Commented Sep 6, 2012 at 8:38
  • 1
    the link seems to work fine, but the file is a json data that must be received by a ajax or something(it depends on what you really want to do) Commented Sep 6, 2012 at 8:40

6 Answers 6

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>
3
  • Tried but didn't work, please check my edit Commented Sep 6, 2012 at 8:48
  • @user1641732 Have a peep at the edit and see if that is it? Commented Sep 6, 2012 at 8:55
  • I moved the scripts around and it worked. Thanks ever so much for your help. Commented Sep 6, 2012 at 9:10
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>
1
  • Tried but didn't work, check my edit to see my code, thanks Commented Sep 6, 2012 at 8:50
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>
2
  • Tried but didn't work, check my edit to see my code, thanks Commented Sep 6, 2012 at 8:49
  • You have : <body> <?php echo $string; ?> , here works ? Commented Sep 6, 2012 at 8:59
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?

5
  • Tried but didn't work, check my edit to see my code, thanks Commented Sep 6, 2012 at 8:47
  • @user1641732 : It Seems you don't understand, so once again.Post GENERATED HTML code, not source.Also, what you are expecting from your code? Commented Sep 6, 2012 at 8:50
  • I get a 500 internal server error with my string printed out on the page: Commented Sep 6, 2012 at 8:53
  • 500 means internal server error, neither javascript nor PHP problem. Commented Sep 6, 2012 at 8:55
  • From experience this normally happens when there is an error in my php code. Commented Sep 6, 2012 at 8:58
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);
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>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.