Flaskに対しphpからAPIコールし値を渡す方法

■GET

PHP側

$url = 'https://xxxxxxxxxx';
$params = ['para1' => 'xxxx', 'para2' => 'yyy'];

$queryString = http_build_query($params);
$fullUrl = $url . '?' . $queryString;

$response = file_get_contents($fullUrl, false);

Flask側

@app.route('/')
def xxxx():
    para1 = request.args.get('para1')
    print(para1)
    para2 = request.args.get('para2')
    print(para2)

    result = 'ok'
    return jsonify({'result': result})

■POST

PHP側

$url = 'https://xxxxxxx';
$data = ['para1' => 'xxxx', 'para2' => 'yyyy'];

$options = [
    'http' => [
        'method' => 'POST',
        'header' => [
            'Content-Type: application/x-www-form-urlencoded',
        ],
        'content' => http_build_query($data),
    ],
];

$context = stream_context_create($options);

$response = file_get_contents($url, false, $context

Flask側

@app.route('/', methods=['POST'])
def test():
    para1 = request.form.get('para1')
    print(para1)
    para2 = request.form.get('para2')
    print(para2)

    result = 'ok'
    return jsonify({'result': result})

返信を残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です