【发布时间】:2016-02-18 19:41:46
【问题描述】:
我有一个 php 文件,其中有 API 代码。该文件使用 twitter api,在文件中我通过会话传递用户密钥以处理和发送推文。
我想知道,当多个请求同时到达这个文件时会发生什么?
如果来自不同地理位置的 5 个请求进入该文件..每个请求具有不同的变量值...但都将采用相同的变量名称...php 是否创建该文件的 5 个实例来执行推文?
请查看我的代码以了解发生了什么:http://pastebin.com/ic6pYLgi
当多人向该文件发送请求时会发生什么?
<?php
session_start();
//pass session variables into standard variable
$ck = $_SESSION['consumer_key'];
$cs = $_SESSION['consumer_secret'];
$at = $_SESSION['access_token'];
$ats = $_SESSION['access_token_secret'];
$hashtag = $_SESSION['hashtag'];
$tweet = $_POST['tweet'];
//TODO unset session at end of script
//TODO limit tweet length
if (!empty($ck && $cs && $at && $ats && $tweet)){
$ck = $_POST['consumer_key'] = $ck; //get session value into POST then pass back to variable
$cs = $_POST['consumer_secret'] = $cs;
$at = $_POST['access_token'] = $at;
$ats = $_POST['access_token_secret'] = $ats;
$_POST['tweet'];
$hashtag = $_POST['hashtag'] = $hashtag;
}
require "autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', $ck); // add app consumer key between single quotes
define('CONSUMER_SECRET', $cs); // add app consumer secret key between single quotes
$access_token = $at;
$access_token_secret = $ats;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
if (!empty($ck && $cs && $at && $ats && $tweet && $hashtag)) {
$hashtag = '#' . $hashtag;
$connection->post('statuses/update',array('status' => $tweet . ' ' . $hashtag));
$t = $connection;
echo '<pre>';
//print_r($t);
echo '</pre>';
echo "Your message has been sent to Twitter with a hashtag.";
}
//TODO Add error handling to send to special inbox
//Tweet without hashtags
if (!empty($ck && $cs && $at && $ats && $tweet) && empty($hashtag)) {
$connection->post('statuses/update',array('status' => $tweet));
//$t = $connection;
//echo '<pre>';
//print_r($t);
//echo '</pre>';
//echo "Your message has been sent to Twitter without a hashtag.";
}
session_destroy(); //this will hopefully help with load on this file
?>
【问题讨论】:
-
"php 是否创建该文件的 5 个实例" =yes.
-
@Dagon 你查看代码了吗?如果你说的是真的,那我就是金子。
-
当人们同时访问同一个网页时会发生什么——同样的事情
-
session_destroy() 的使用是可疑的,并且可能没有按照您的想法执行。 php.net/manual/en/function.session-destroy.php
标签: php twitter server request ram