像//google.com
这样的东西不适用于file_get_contents
是否有一个解决方案不需要将协议添加到字符串中?
答案 0 :(得分:1)
您不能将file_get_contents
与//google.com
一起使用,因为当您在网络浏览器中执行此操作时,它实际上正在执行的操作实际上是使用您当前使用的当前协议。因此,如果您有file:///google.com
并且您将某些内容与https://mywebsite.com
相关联,那么它实际上会//google.com
。话虽如此,你需要做https://google.com
答案 1 :(得分:-1)
我认为提供用class Server {
public static final int port = 11111;
private final ServerSocket mServerSocket;
private final Collection<Client> mClients;
private boolean acceptNewClients;
public Server() {
mServerSocket = new ServerSocket(port);
mClients = new ArrayList<Client>();
acceptNewClients = true;
ThreadManager.t.execute(new Runnable() {
@Override
public void run() {
Client c;
while (acceptNewClients) {
// On new client connected
c = new Client(mServerSocket.accept());
mClients.add(c);
}
}
});
}
private void broadcast(String message) {
for (Client c : mClients)
c.sendMessage(message);
}
}
class ThreadManager {
public static final ExecutorService t = Executors.newCachedThreadPool();
}
final class Client implements Runnable {
private Socket mSocket;
private OutputStreamWriter mWriter;
private InputStreamReader mReader;
private boolean mContinueNetworking;
private LinkedBlockingDeque<String> mCommandsToSend;
public Client(Socket s) {
mSocket = s;
mWriter = new OutputStreamWriter(socket.getOutputStream());
mReader = new InputStreamReader(socket.getInputStream());
mCommandsToSend = new LinkedBlockingDeque<String>();
mContinueNetworking = true;
ThreadManager.t.execute(this);
}
public void sendMessage(String message) {
mCommandsToSend.addLast(message);
}
@Override
public void run() {
String message;
while (mContinueNetworking) {
message = mCommandsToSend.take();
try {
mWriter.write(message);
mWriter.write('\n');
mWriter.flush();
} catch (SocketException e) {
e.printStackTrace();
mContinueNetworking = false;
} catch (IOException e) {
e.printStackTrace();
// optional: uncomment for retry in case of failure
// commandsToSend.put(toSend);
}
}
}
}
加载资源的协议是必要的。此功能在服务器上评估,而不是在客户端浏览器上进行评估,其中使用//自动协议检查。
为什么你不能通过协议?
如果你仍然需要根据对服务器的实际请求来决定协议,你可以查看环境变量$ _SERVER a,如果https放在那里https或反之亦然。