简体   繁体   English

我该如何像在Python中那样执行PHP请求,以JSON格式提供数据

[英]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.

相关问题 我有此查询,使我得到以下php数组数据: - I have this query that gives me the following php array data: 我怎样才能用 PHP 编写一个看起来像这样的 JSON 文件? - How can i write an JSON file with PHP which looks like this? 我有一个PHP代码,在执行时会给出错误。 谁能指出我的错误。 - I have a PHP code which gives and error when executing. Can anyone point me out the error please. json_encode()提供特殊字符,例如'\\ n'。如何在PHP中替换它们? - json_encode() gives special charactors like '\n'..how can i replace them in PHP? 我怎样才能用 PHP 编写一个看起来像这样的 JSON 文件(嵌套的 JSON 结构)? - How can i write an JSON file with PHP which looks like this (nested JSON structure)? 我有一个提供JSON输出的php文件,并想在D3中使用该JSON - I have a php file which gives JSON output and want to use that JSON in D3 如果我已经拥有IDE,Web主机和FTP客户端,是否需要安装PHP,MySQL和类似XAMP的软件? - Do I need to install PHP, MySQL, and something like XAMP, if I already have an IDE, a webhost, and FTP client? 如何更新PHP数据库中已经存在的记录 - How can i update the records which is already in a database in php 使用mySQL,Php和JS,如何请求dans使用我在数据库中的数据? - Using mySQL, Php and JS, how do I request dans use data I have in my DB? 在CodeIgniter中,如何通过电子邮件发送给我的PHP错误消息? - In CodeIgniter, How Can I Have PHP Error Messages Emailed to Me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM