Send file using wp_safe_remote_post
-
On my local tests, I’m using the follow code to send a file on HTTP connection:
$url = 'https://...'; $ch = curl_init(); $tmpfile = 'teste.txt'; $curl_file = new CURLFile(realpath($tmpfile),'text/csv',$tmpfile); $file = array('file[]' => $curl_file); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $file); //$data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($ch); if(curl_error($ch)) { echo 'error:' . curl_error($ch); } else { echo $response; }
This code works perfectly!
Now, I wanna connect to this URL from my WordPress application.
The problem is that I don’t know how to send files using the wp_safe_remote_post.
I tried the following code:
$url = 'https://...'; $tmpfile = WP_CONTENT_DIR . '/test.txt'; $params = array( 'method' => 'POST', 'timeout' => 60, 'body' => [ 'file[]' => realpath( $tmpfile ), ] ); $response = wp_safe_remote_post( $url, $params );
But the remote server says that no file was sended!
What I did wrong? How to make this work?
Thanks for your help!
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Send file using wp_safe_remote_post’ is closed to new replies.