1

I read this question, but it is not relevant to my question, so thought I would ask this here.

I am sending a simple text file that contains the words HELLO WORLD using a PHP curl script to a python flask server:

<?php

$url='http://127.0.0.1:5000/transaction';
$ch = curl_init($url);
$cfile = ['file_contents' => curl_file_create ('sampledata.txt')];

$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);

?>

Flask code:

@app.route('/transaction', methods=['POST'])
def read_file():
    if (request.method == 'POST'):
        return request.data
    else:
        abort(500) 

This gives an HTTP 200 status code on the Flask terminal, but on the terminal where I execute the PHP script, it says: Notice: Array to string conversion in C:\..path..\testscript.php on line 9

(Line 9 happens to be where I set the CURLOPT_POSTFIELDS, $data option)

How do I output to screen the contents of the text file in this scenario?

omrakhur
  • 1,362
  • 2
  • 24
  • 48

0 Answers0