[英]How can I do a PHP request like I already have in Python which gives me data in JSON
I have a Pythonscript wich gives me back some data in JSON. 我有一个Python脚本,可以给我一些JSON数据。 It starts a session, post some auth data and request data which comes back with JSON.
它开始一个会话,发布一些身份验证数据并请求JSON返回的数据。 That works fine, but can somebody help me to do this in PHP?
效果很好,但是有人可以帮助我在PHP中执行此操作吗? I am sure it is possible but I am struggling to construct that.
我确信这是可能的,但我正在努力构建。
import requests
with requests.Session() as s:
start_time = time.time()
s.post('http://IP:PORT/WHATEVER/AUTH', data={'username':'jdoe','password':'forgotten'})
req = 'http://IP:PORT/WHATEVER/DATA/BOM%20fe?table=cars,tires,&format=json'
res = s.get(req)
you need to look for curl request in php. 您需要在php中查找curl请求。 here is a example
这是一个例子
function getdata($url){
if(!function_exists("curl_init"))
die("cURL extension is not installed");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
return $output;
}
$output = getdata("https://website.com");
var_dump($output);
you can use json decode fuction to covert into matrix and use the variables 您可以使用json解码功能将其转换为矩阵并使用变量
$arr = json_decode($output,true);
you can use something like this for formatting: 您可以使用以下格式进行格式化:
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(array('postvar1' => 'value1')));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.