-2

当我手动将 URL 字符串放入参数中时,我有以下功能可以完美运行。我需要它是动态的,我正在使用 Wordpress。

    function get_tweets($url) {     
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' .  $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
// Below is the one that works manually     
<?php echo get_tweets('http://www.someurl.com');

//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();

echo get_tweets('$url');
echo get_tweets($url);

$url = '$get_permalink()'; 
$url = $get_permalink(); // produces needs to be in string error
echo get_tweets($url);
4

3 回答 3

0

你所做的事情本身并没有错。我能看到的唯一明显错误是您没有正确编码 URL。您需要确保您在 URL 中输入的查询字符串参数经过正确的 URL 编码,否则远程主机可能无法正确解释请求。

function get_tweets($url) {     
    $json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' .  urlencode($url));
    $json = json_decode($json_string, true);
    return intval( $json['count'] );
}

echo get_tweets('http://www.someurl.com'); // should work just fine
于 2012-12-07T11:58:48.543 回答
0

您是否尝试对您的 url 字符串进行urlencode 编码?

urlencode($foo);
于 2012-12-07T11:59:47.173 回答
0

您的主要问题在下面一行

改变

//ones I have tried that do not (trying to make dynamic)
$url = $get_permalink();

//ones I have tried that do not (trying to make dynamic)
$url = get_permalink();
于 2012-12-07T12:06:44.290 回答