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.
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 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
I've a tricky question on how to deal with consecutive curls in PHP. I have this incredibile data flow:
example.com/one.php post data via curl to another.com/two.php
another.com/two.php post data via curl to another.com/three.php
another.com/three.php responds me with XML (or JSON) back to another.com/two.php
another.php/two.php transforms the XML into a php array and then into a query string that i post via curl back to the origin example.com/one.php
It works. If you are wondering why i have this insane data flow it's due to the fact that another.com/three.php is an obfuscated file with Ioncube. I can't edit it but i have to add some checks before i can send data to him. Don't waste time trying to figure out how i can make it in an alternative way because there isn't one (trust me).
On example.com/one.php there's a form where users fill in data. When they press "Submit" they remain on this page while "silently" i make 1->2->3->4 to get their response (the $_POST of step 4) then can save it into example.com/log.txt. Again it works.
Now my question is: how can i display the $_POST response (which is the same i get in log.txt file) in example.com/one.php? I mean this is what i have in step 4.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, example.com/one.php);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$xml = curl_exec($ch);
$_POST arrives on example.com/one.php but of course users and this curl are on two different level. I tryed playing with file_get_contents(), sleep() and CURLOPT_RETURNTRANSFER with no success. What's the answer this this question? I would go for sessions or ob_start() but i'm not sure of it.
I'm trying to scrape a website that takes in POST data to return the correct page (sans POST it returns 15 results, with POST data it returns all results).
Currently my code is looking like this:
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"http://www.thisismyurl.com/awesome");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, XXXXXX);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result= curl_exec($curl);
I know that I need to put my postfields into the space filled with "XXXXXX", I just don't know where to dig up the post fields/values and how to structure them into the variable that I pass into there.
Any help would be greatly appreciated!
If it's a simple form, then just extract all the form fields and duplicate them in your script. If it's some dynamic form, like javascript building up a request and using ajax, then you can sniff the data using developer tools (e.g. Firefox's Firebug Net tab, HTTPfox, etc...) and extract the post data as it gets sent over.
Either way, once you know what fields/data are being sent, the rest should be (relatively) easy to duplicate/build.
I think someone may look for code to replace XXXXXX. I use the following piece of code.
$ch = curl_init();
$timeout=5;
$name=$_REQUEST['name'];
$pass=$_REQUEST['pass'];
$data = array('username' => '$name', 'password' => '$pass');
$data=http_build_query($data);
curl_setopt($ch,CURLOPT_URL,"superawsomesite.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
Can anyone kindly tell me how to use JSON base Authentication using/in PHP?
I got some helping code (as given below), but can not understand that how to use it.
url = http://testerws.heroku.com/
POST url + 'user_session.json',{:username => "admin",:password => "admin"}, :accept => 'application/json'
I am not sure whether this code is correct (with respect to syntax) or not. And I can not understand how to use it in combination with PHP.
So kindly help me to solve this, I am in great need of it.
You can use curl of php to post the json data.
$json = json_encode($data);
$http_post_data = array("data" => $json);
$url = 'http://testerws.heroku.com/test.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $http_post_data);
$result = curl_exec($ch);
curl_close($ch);
Then test.php will get the data which you post. You can do authentication your self and return to the calling script.
But the authentication is not that easy for a security system.
FYR.
http://blog.evernote.com/tech/2011/05/17/architectural-digest/
and read the comment.
What is the context of the authentification? If it's a webapplication you could probably fill in a form, post the data as JSON using JQuery and decoding the JSON on the PHP side and check the validation. Be sure to encrypt the information you send from the browser though. Username and password as clear text is not secure at all.