【发布时间】:2015-10-04 19:00:14
【问题描述】:
如何在另一个通过 ajax 调用的 php 文件中引用在一个 php 文件中创建的类对象?
我有一个项目使用 Tumblr 的 API 在博客上发帖
有 2 个文件:tumblr.php 和 ajaxTumblr.php
在tumblr.php 中,创建了一个 Tumblr 对象:
include_once ("Tumblr/API/Client.php");
include_once ("Tumblr/API/RequestException.php");
include_once ("Tumblr/API/RequestHandler.php");
$consumerKey = "xxxxxx";
$consumerSecret = "xxxxxx";
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
然后它通过 oauth 序列,最终以完全填充了访问令牌和 token_secret 的$client 结束。我省略了代码,因为它可能不相关。
现在用户已通过验证,我可以执行显示用户信息等操作
$info = $client->getUserInfo();
或者甚至使用一些预设数据发布帖子。
$postData = array('title' => $title, 'body' => $body, 'format' => $format);
$client->createPost($userid, $postData);
到目前为止,一切都很好。
现在我通过 textareas 从用户那里收集数据(标题和博客文本),并使用此信息向 ajaxTumblr.php 发送 ajax 请求
$.ajax({
url: 'http://domain.com/ajaxTumblr.php',
type:'POST',
async: "false",
data: {'title': title,
'body': body,
'userid': userid,
'state': "draft",
'format': "html" },
success: function (res) {
console.log (res);
}
});
这就是我卡住的地方。
我们如何在 ajax php 文件中传递、引用或重新创建 $client?我无法生成新的 Tumblr 对象,因为它需要用户授权。
我想让我的代码执行如下操作:
$title = $_REQUEST['title'];
$body = $_REQUEST['body'];
$format = $_REQUEST['format'];
$userid = $_REQUEST['userid'];
$postData = array('title' => $title, 'body' => $body, 'format' => $format);
$client->createPost($userid, $postData);
谢谢。
更新
在Tumblr.php,我保存了$client:
$_SESSION['client'] = serialize($client);
然后在ajaxTumblr.php 中创建了一个新对象并将原来的对象复制到新的对象中。这样做可以吗?
include_once ("Tumblr/API/Client.php");
include_once ("Tumblr/API/RequestException.php");
include_once ("Tumblr/API/RequestHandler.php");
// OAuth Consumer Key:
$consumerKey = "xxxxxxx";
$consumerSecret = "xxxxxxxx";
$ajaxClient = new Tumblr\API\Client($consumerKey, $consumerSecret);
$client = unserialize($_SESSION['client']);
$ajaxClient = $client;
当我重新运行测试时,其中一个类模块引发了数千个错误,抱怨参数不正确。我可能走在正确的道路上,但需要确认。
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_add_handle() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 181
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_exec() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 238
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_info_read() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 254
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_exec() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 238
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_info_read() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 254
[04-Oct-2015 14:46:27 America/Chicago] PHP Warning: curl_multi_exec() expects parameter 1 to be resource, integer given in /home3/me/vendor/guzzle/guzzle/src/Guzzle/Http/Curl/CurlMulti.php on line 238
【问题讨论】: