Вопрос

I'm using the Bit.ly API to generate a shorturl of my domain, and then I need to pass it as a javascript variable. Unfortunately When the short url is generated, it causes an "Uncaught SyntaxError: Unexpected token ILLEGAL" Here is my source code

<?php
function get_bitly_short_url($url,$login,$appkey,$format='txt')
{ $connectURL = 'http://api.j.mp/v3/shorten?login='.$login.'&apiKey='.$appkey.'&uri='.urlencode($url).'&format='.$format; return curl_get_result($connectURL);}
function curl_get_result($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$short_url = get_bitly_short_url('http://mydomain.com','BitLyUserName','ApiKey');
?>
<script>
var site = "<?php echo $short_url ?>";
var text = "Something else"
</script>

Please take a look and help me, I really need it

Это было полезно?

Решение

I believe you need to trim() the return data. I tested it out and curl was pushing in a newline character after the url, so the js was being output like:

var site = "http://j.mp/bMSmZV
";

Update the return from your function to be

return trim($data);

and you should be good to go.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top