简介
我编写了以下PHP
代码,以根据IP address
查找位置。但是此代码在客户端正常工作,并且在服务器上不起作用。可能是什么问题?
代码
echo $ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo "<br>".$details->country;
echo "<br>".$details->region;
echo "<br>".$details->city;
echo "<br>".$details->loc;
echo "<br>".$details->postal;
echo "<br>".$details->org;
问题
此代码仅显示ip address
(仅在第一行执行),并且不显示任何其他位置详情。
答案 0 :(得分:4)
没有看到您的错误消息,我将继续并假设import paho.mqtt.client as mqtt
import json
import time
is_connect = False
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print("Message received on "+msg.topic)
parsed_message = json.loads(msg.payload)
print(json.dumps(parsed_message, indent=4, sort_keys=True))
def on_disconnect(client, userdata, rc):
if rc != 0:
print "Unexpected disconnection." , (rc)
global is_connect
is_connect = False
while True:
global is_connect
#If client is not connected, initiate connection again
if not is_connect:
try:
client = mqtt.Client(client_id='testing_purpose', clean_session=False)
client.loop_stop()
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.connect("localhost", 1883, 5)
is_connect = True
client.loop_start()
time.sleep(15)
except Exception as err:
is_connect = False
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
#client.loop_forever()
已被阻止。
CURL网址,更加可靠。
file_get_contents
答案 1 :(得分:0)
您的服务器的IP地址如下:
echo $_SERVER['SERVER_ADDR'];
$_SERVER['REMOTE_ADDR'];
为您提供客户端或请求浏览器/计算机的IP地址。