Twitter API 无法托管 [在本地主机上运行良好]
Twitter APIs not working on hosting [Works fine on localhost]
我正在开发一个应用程序来显示在给定地理位置(纬度和经度)的 1 英里半径内完成的推文。这是我的 PHP 代码,
<?php
// $lat = $_GET['lat'];
$lat = 26.511740;
// $long = $_GET['long'];
$long = 80.234973;
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$notweets = 100;
$consumerkey = "XXXX";
$consumersecret = "XXXX";
$accesstoken = "XXXX-XXXX";
$accesstokensecret = "XXXX";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey,$consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?geocode=".$lat.",".$long.",5mi&result_type=recent&count=".$notweets);
// echo $tweets;
echo json_encode($tweets);
?>
我正在使用 Wamp 服务器 (PHP V5.5.12),我的代码在上面运行良好。但是当我在一些免费托管网站上托管我的应用程序时(我已经尝试过 hostinger.in 和 000webhost.com),这个脚本失败并且只打印 'null'.
请帮我解决这个问题。
提前致谢。
可能与您可用的库有关。
将 error_reporting(E_ALL)
添加到脚本的顶部。
检查廉价主机上是否安装了 cURL,因为我相信这是 twitteroauth 唯一需要的 php 库。
我已经尝试过 hostinger 和 000webhost 以及其他几个。他们不工作的原因是连接到 twitter 的库使用 php curl,并且许多免费托管禁用了 curl,或者传出连接或者 twitter 在来自免费托管服务器的 ips 时拒绝 curl 连接。对于我在互联网上读到的内容,这可能是因为许多黑客一直在搞乱 Twitter 和从免费托管帐户托管。因此,通过 cpanel 找到一个与 Twitter 兼容的免费主机 API 这是一个挑战,我已经尝试了 20 多个但它们不起作用,其中一些会自动删除帐户或文件或阻止 ftp 访问如果你尝试 curl 到 Twitter
你的 TwitterOAuth
版本是什么?您的代码似乎太旧了,与最新版本不兼容。
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
$to = new TwitterOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
if (isset($tweets->errors[0]->message)) {
echo 'Error: ' . $tweets->errors[0]->message;
} elseif (!is_array($tweets)) {
echo 'Unknown Error';
} else {
echo '<pre>';
var_dump($tweets);
echo '</pre>';
}
或者,您可以使用 TwistOAuth
而不是 TwitterOAuth
。我是这个图书馆的作者。这个库几乎与 TwitterOAuth
兼容,但支持严格的异常处理。 错误原因总是要清楚的。
<?php
require 'TwistOAuth.phar'; // Or 'vendor/autoload.php' for composer
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
try {
$to = new TwistOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
echo '<pre>';
var_dump($tweets);
echo '</pre>';
} catch (TwistException $e) {
echo 'Error: ' . $e->getMessage();
}
你喜欢哪个代码?
我正在开发一个应用程序来显示在给定地理位置(纬度和经度)的 1 英里半径内完成的推文。这是我的 PHP 代码,
<?php
// $lat = $_GET['lat'];
$lat = 26.511740;
// $long = $_GET['long'];
$long = 80.234973;
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$notweets = 100;
$consumerkey = "XXXX";
$consumersecret = "XXXX";
$accesstoken = "XXXX-XXXX";
$accesstokensecret = "XXXX";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey,$consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?geocode=".$lat.",".$long.",5mi&result_type=recent&count=".$notweets);
// echo $tweets;
echo json_encode($tweets);
?>
我正在使用 Wamp 服务器 (PHP V5.5.12),我的代码在上面运行良好。但是当我在一些免费托管网站上托管我的应用程序时(我已经尝试过 hostinger.in 和 000webhost.com),这个脚本失败并且只打印 'null'.
请帮我解决这个问题。
提前致谢。
可能与您可用的库有关。
将 error_reporting(E_ALL)
添加到脚本的顶部。
检查廉价主机上是否安装了 cURL,因为我相信这是 twitteroauth 唯一需要的 php 库。
我已经尝试过 hostinger 和 000webhost 以及其他几个。他们不工作的原因是连接到 twitter 的库使用 php curl,并且许多免费托管禁用了 curl,或者传出连接或者 twitter 在来自免费托管服务器的 ips 时拒绝 curl 连接。对于我在互联网上读到的内容,这可能是因为许多黑客一直在搞乱 Twitter 和从免费托管帐户托管。因此,通过 cpanel 找到一个与 Twitter 兼容的免费主机 API 这是一个挑战,我已经尝试了 20 多个但它们不起作用,其中一些会自动删除帐户或文件或阻止 ftp 访问如果你尝试 curl 到 Twitter
你的 TwitterOAuth
版本是什么?您的代码似乎太旧了,与最新版本不兼容。
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
$to = new TwitterOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
if (isset($tweets->errors[0]->message)) {
echo 'Error: ' . $tweets->errors[0]->message;
} elseif (!is_array($tweets)) {
echo 'Unknown Error';
} else {
echo '<pre>';
var_dump($tweets);
echo '</pre>';
}
或者,您可以使用 TwistOAuth
而不是 TwitterOAuth
。我是这个图书馆的作者。这个库几乎与 TwitterOAuth
兼容,但支持严格的异常处理。 错误原因总是要清楚的。
<?php
require 'TwistOAuth.phar'; // Or 'vendor/autoload.php' for composer
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
try {
$to = new TwistOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
echo '<pre>';
var_dump($tweets);
echo '</pre>';
} catch (TwistException $e) {
echo 'Error: ' . $e->getMessage();
}
你喜欢哪个代码?