I'm trying to send data via cURL post but I've never tried it before and I don't know if I'm doing it right.
What I want to do is sent a file via post to a file from my remote server and there read the file and insert data into database but unfortunately it's not working and error_log doesn't show me anything.
My code looks like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://".$host."/file.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'file' => '#'.realpath(../path/to/file/'.$_POST['file_name'].'.txt'),
'action' => 'first',
'check' => $_POST['file_name'],
));
$result = curl_exec($ch);
curl_close($ch);
This code is placed after some sql querys and code made to write this sql results into the file that I'm trying to send.
Here is the Answer by Shakir Khan you Should follow:
PHP: upload file from one server to another server - Stack Overflow
OR you should read PHP Documentation carefully there is a huge amount of CONSTANT availabe to use: curl_setopt
Related
I have to automate uploading values to a remote server by php script.
The interface accepts only POST variables - NO GET variables.
Interface documentation (for form field names) looks like:
iegUsername: your username
iegPassword: your password
iegImportFile: The UTF-8 encoded plain text import file. The file data may optionally be zipped.
This means i have to POST TWO VARIABLES and ONE FILE.
I'm new in Curl therefore i started in PHP with this:
// create curl resource
$request = curl_init();
// Array with the fields names and values
$postData = array(
'iegUsername' => 'myusername',
'iegPassword' => 'mypassword',
'submit' => 'ok'
);
//add file support with: 'file' => '#' . realpath('example.txt')
// set timeout if remote server is down
curl_setopt($request, CURLOPT_CONNECTTIMEOUT, 30);
// set remote target url
curl_setopt($request, CURLOPT_URL, ""); //for testing post on current page
//Enable the post response.
//curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
$headers = ['Content-Type: multipart/form-data'];
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
//The data to transfer with the response.
curl_setopt($request, CURLOPT_POSTFIELDS, $postData);
//return the transfer as a string
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
// disable verification
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($request, CURLOPT_SSL_VERIFYHOST, false);
Actually i don't have added file support.
My Problem
In Browser dev tools i cannot see any POST only GET responses.
I need a real POST with data in it not a urled GET one...
Hopefully someone can give help.
okay, misunderstood of server side and client side. Thought a response back to my location should result in a post response within dev tools of browser, this is not correct.
POST works.
I'm using PHP to post a few files to another PHP application. I need to send multiple files in a request. I am putting
["happyfacepng"]=> string(36) "#/var/www/html/imgtest/happyface.png"
Into the post array (as well as some other files in the same directory with different key names) and posting them using:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$node."/rendernodeinterface.php");
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 0);
$result = curl_exec($ch); //Post this to the node
curl_close($ch);
And the server on the other end has a blank $_FILES , but the post array has
["happyfacepng"]=> string(36) "#/var/www/html/imgtest/happyface.png"
in it as well as the other non-file data and whatnot. So the sender isn't sending the files along with post. What am I missing here that's preventing cURL from actually posting the contents of these files?
Thank you for helping, I feel like there's a really simple answer to this question that I just can't figure out.
I have a web-service that accepts file as a Post request and upload it on the server and returns the file path.
The service is working properly though Ajax request. But now i want to make a request from Php to the web service and its giving me error that "Undefined index: imageFile"
The code i am trying to use is below
$filename = 'e:/e.jpg';
$fields = array('imageFile'=>$filename);
$fieldsQueryString = http_build_query($fields);
// you should have curl library install here
$ch = curl_init("http://127.0.0.1/uploadapi/index.php/api/example/imageupload"); curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
You should add # (eta) sign before file path to upload it.
For more, read at PHP manual for curl_setopt for CURLOPT_POSTFIELDS setting.
<?php
$filename = '#e:/e.jpg';
$fields = array('imageFile'=>$filename);
$ch = curl_init("http://127.0.0.1/uploadapi/index.php/api/example/imageupload");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
I have to use
curl_file_create()
object to create the file and send it to the web service. Just did it and its working like a charm
Thanks
So I'm rather new to using cURL in PHP, I was told to use it for a task this week and it's been nothing but a pain, and I can't seem to find a solution to my problem no matter how hard I search.
What I am attempting to do is send a file to an upload directory on my hosted server from a remote portal manager that I have built. The file uploadhandler in the portal manager connects via curl to the remote destination and then the remote destination grabs the info and processes the file like normal. No matter what I've been trying though everything just throws back a failed response.
Here is the updated version of the code I am working with
updates
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
//curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
array(
'file' =>
'#' .$_FILES['doc']['tmp_name'][$i]
.';filename=' .$_FILES['doc']['name'][$i]
.';type=' .$_FILES['doc']['type'][$i]
)
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//$response = curl_exec($ch);
if(curl_exec($ch) === false){
echo curl_error($ch);
echo curl_errno($ch);
}else{
echo "ok";
}
With all of this information set I am getting no value for curl_error but I get a value of 43 for curl_errno.
From what I have been researching, error 43 for curl is
CURLE_BAD_FUNCTION_ARGUMENT (43)
Internal error. A function was called with a bad parameter.
However all my functions for the curl_setopt() are put together correctly based on the info from php.net. So this is where I am now confused, because I have no idea what is causing this to happen. Thanks again for the help!
I use the following command in some old scripts:
curl -Lk "https:www.example.com/stuff/api.php?"
I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.
What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.
I suspect something in the order of :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// What goes here so that I just get the Location and nothing else?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
The goal being $response = the data from the api “OK=1&ect”
Thank you
I'm a little confused by your comment:
// What goes here so that I just get the Location and nothing else?
Anyway, if you want to obtain the response body from the remote server, use:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
If you want to get the headers in the response (i.e.: what your comment might be referring to):
curl_setopt($ch, CURLOPT_HEADER, 1);
If your problem is that there is a redirection between the initial call and the response, use:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);