我正在尝试向 Twitter API 发出获取请求,以消除混乱,并且在 url 中我有一个变量,但我无法让它连接起来


I'm trying to make a get request to a Twitter API that eliminates clutter and in the url I have a variable and I can't get it to concatenate?

<?php
session_start();
require_once("twitteroauth/twitteroauth.php");
$apikey="xxx";
$apisecret="xxx";
$accesstoken="xxx";
$accesssecret="xxx";
$connection = new TwitterOAuth($apikey, $apisecret, $accesstoken, $accesssecret);

$response= 
$connection->get("https://api.twitter.com/1.1/statuses/home_timeline.json?count=10");
foreach ($response as $tweet) {
        $favorites=$tweet->favorite_count;
            if ($favorites>1) {
    $embed = $connection->get("https://api.twitter.com/1.1/statuses/oembed.json?id='.$tweet->id.'");
    print_r($embed->html);
                    }
    } 
?>

它与代替 $tweet->id 的示例推文 id# 一起使用,我已经尝试了很多不同的语法来尝试使这个小应用程序工作。我是新来的,谢谢。

尝试取出您在$tweet-id之前和之后的点。有两种方法可以将值内联放入其中:

$embed = $connection->get("https://api.twitter.com/1.1/statuses/oembed.json?id='$tweet->id'");

(当用双引号"将字符串括起来时,您可以像任何其他文本一样直接在其中输入变量。

$embed = $connection->get("https://api.twitter.com/1.1/statuses/oembed.json?id='".$tweet->id."'");

(在此方法中,您可以明确表示要在其中添加一个变量。请注意,当使用单引号'时,必须使用此方法。

相关文章: