Send file data using PHP HttpRequest

I have data that is contained in a variable (which can be written as a file), but I want to send it to another server using PHP HttpRequest

.

I thought about using it addRawPostData

, but now I am out of date and I would rather not write the file to the client server and then try and send it. If anyone has any ideas, let me know. I also tried to represent the original data from a variable to a field in a normal one post

, but it seems to be corrupted.

The file I am trying to upload is a torrent file, if that matters.

Thanks again

+3


source to share


3 answers


You can use cURL , this is a simple HTTP POST example:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.site.com/url");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"key=value&key1=value1");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

curl_close ($ch);

// Check response
if ($server_output == "OK") { 

      



There is an example of POSTing a line of a file using cURL in this answer

+2


source


To send a file using HttpRequest: http://www.php.net/manual/en/httprequest.addpostfile.php To add some raw data: http://www.php.net/manual/en/httprequest.addrawpostdata.php - HttpRequest::addRawPostData

not deprecated but HttpRequest::setRawPostData

deprecated.



However, HttpRequest is only a cURL wrapper and in some cases it would be better to use it cURL

directly.

+1


source


use setBody to send rawpost data http://www.php.net/manual/en/httprequest.setbody.php

0


source







All Articles