請(qǐng)教在webman中如何獲取服務(wù)器本身的IP地址?我使用了 $_SERVER['SERVER_ADDR' ] 會(huì)報(bào)錯(cuò),說(shuō)沒(méi)有這個(gè)屬性。
那么正確的獲取服務(wù)器本身的IP的方法是什么呢?現(xiàn)在我沒(méi)辦法,直接硬編碼了。
public function share($data): array {
$token = Redis::hGet(config('weibo.redis_key'), 'access_token');
$client = new Client();
try {
$response = $client->request('POST', config('weibo.share_url'), [
'form_params' => [
'access_token' => $token,
'status' => $data['content'],
'rip' => '192.168.1.1'
]
])->getBody()->getContents();
return json_decode($response, true);
} catch (GuzzleException $e) {
throw new BizException(ErrorCode::WEIBO_ERR_CODE, ['message' => $e->getMessage()]);
}
}
現(xiàn)在按 nitron 建議,我把rip部分換成 'rip' => request()->getLocalIp()
, 執(zhí)行后提示
Error: Call to a member function getLocalIp() on null
上述代碼是在redis-queue里調(diào)用的
class ShareWeiboSend implements Consumer
{
/**
* @Inject
* @var WeiBoService
*/
private $weiBoService ;
// 要消費(fèi)的隊(duì)列名
public $queue = 'share_weibo';
// 連接名,對(duì)應(yīng) plugin/webman/redis-queue/redis.php 里的連接`
public $connection = 'default';
public function consume($data) {
$content['content'] = $this->weiBoService->makeShareContent($data);
try {
$this->weiBoService->share($content);
} catch (BizException $e) {
Log::error($e->getMessage());
}
}
}