如何从 php 中的异常端口获取 JSON 数据?
how to get JSON data from unusual port in php?
我有 scout_realtime,我想从中得到 stats.json。在调试中它看起来像
91.205.168.39 - - [10/Nov/2018:09:54:22 CET] "GET /stats.json HTTP/1.1" 200 3896
http://188.165.3.*:5556/ -> /stats.json
如何手动获取?我试着做这样的事情:
$status = file_get_contents('http://188.165.3.*:5556/stats.gson')
但是没用
$url = 'http://188.165.3.*:5556/stats.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
好的,我自己做的:D
您有多种获取数据的可能性。
首先你可以使用cURL。 CURL 非常适合发送请求。正如您在自己的回答中所展示的那样,您只需几行即可发送 HTTP 请求。我不会展示 CURL 示例,因为它已经在您的回答中完成了。
请注意,您可以使用 Guzzle 之类的库来发送请求,这样更容易。
然后,我不确定您的代码中有什么不起作用,但可以使用 file_get_contents 在给定端口上获取数据。这是使用 PHP 7.2.10.
测试的小示例
server.php
<?php
echo 'Hi from server';
client.php
<?php
echo file_get_contents('http://localhost:8080/server.php');
server.php 充当您要攻击的服务器。它应该是 return 一个简单的字符串。
client.php 是试图获取数据的脚本。
那么如果你运行php -S localhost:8080 -t .
在server.php所在的目录下,就可以执行客户端了
请注意,您可以配置 file_get_contents by giving it a context parameter. See the stream_context_create 文档的行为以了解更多信息。
This answer aims to show that it is possible to fetch datas using file_get_contents, but if you need some specific parameters to fetch your datas like configuring a proxy, a port or something more complexe that just read the file, I suggest to use an HTTP-dedicated function or library, like cURL or Guzzle.
我有 scout_realtime,我想从中得到 stats.json。在调试中它看起来像
91.205.168.39 - - [10/Nov/2018:09:54:22 CET] "GET /stats.json HTTP/1.1" 200 3896
http://188.165.3.*:5556/ -> /stats.json
如何手动获取?我试着做这样的事情:
$status = file_get_contents('http://188.165.3.*:5556/stats.gson')
但是没用
$url = 'http://188.165.3.*:5556/stats.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
好的,我自己做的:D
您有多种获取数据的可能性。
首先你可以使用cURL。 CURL 非常适合发送请求。正如您在自己的回答中所展示的那样,您只需几行即可发送 HTTP 请求。我不会展示 CURL 示例,因为它已经在您的回答中完成了。
请注意,您可以使用 Guzzle 之类的库来发送请求,这样更容易。
然后,我不确定您的代码中有什么不起作用,但可以使用 file_get_contents 在给定端口上获取数据。这是使用 PHP 7.2.10.
测试的小示例server.php
<?php
echo 'Hi from server';
client.php
<?php
echo file_get_contents('http://localhost:8080/server.php');
server.php 充当您要攻击的服务器。它应该是 return 一个简单的字符串。
client.php 是试图获取数据的脚本。
那么如果你运行php -S localhost:8080 -t .
在server.php所在的目录下,就可以执行客户端了
请注意,您可以配置 file_get_contents by giving it a context parameter. See the stream_context_create 文档的行为以了解更多信息。
This answer aims to show that it is possible to fetch datas using file_get_contents, but if you need some specific parameters to fetch your datas like configuring a proxy, a port or something more complexe that just read the file, I suggest to use an HTTP-dedicated function or library, like cURL or Guzzle.