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.
Related
I am trying to set up an Instagram API feed with CURL in PHP, however the string returns empty when trying to retrieve the auth code, before later making a successful call to retrieve a user's content:
$uri = 'https://api.instagram.com/oauth/authorize/';
$data = [
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
However, when I access the URL directly in the browser, it does return to the redirect URI with the code that I need:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT_ID_HERE&redirect_uri=REDIRECT_HERE&response_type=code
i.e. the browser redirects successfully (including the "code" GET data) to http://REDIRECT_HERE/?code=XXXXXXXXXXXX
I'd basically like to store the auth code in a variable for later use rather than using the manual way of getting the code via a browser redirect.
your browser sends the data in the url RFC-1738 style with a GET request, while your php curl code sends the data in a POST request encoded with multipart/form-data, no wonder the server gets confused. to send the data in the url with php just like your browser does, get rid of curl_setopt($ch, CURLOPT_POST, true); (because it tells curl to do a POST request instead of the default GET request, your browser does a GET request) and curl_setopt($ch, CURLOPT_POSTFIELDS, $data); (because it tells curl to send it in the request body, while your browser send it in the url), and change your CURLOPT_URL to
curl_setopt($ch, CURLOPT_URL, $uri."?".http_build_query($data));
then your php curl request should roughly match your browser request, and get the same response. (there will still be minor differences, like a different User-Agent header, Accept header, and Accept-Encoding header, but that usually doesn't matter)
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 am having HTTP cookie based Authentication,After Authentication i get cookie in response,
Another step is to send cookie in request to API. along with post/get request
Below Example is of my Curl
$cookie_jar= $_COOKIE['DesigntoolAuth'];
//$ch = curl_init($url);
// The data to send to the API
$postData = array(
'ProjectId' => $projectId,
'ProjectName' => $proname,
'ProjectDetails' => $details,
'UserId' => $userId
);
$data_string = json_encode($postData);
// Setup cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
//'Content-Length: ' . strlen($data_string)
));
$api_result = curl_exec($ch);
echo $api_result;
Can Anybody tell how ti send the cookie ?
From the cURL PHP reference:
CURLOPT_COOKIEFILE
The name of the file containing the cookie data. The cookie file can be in Netscape format, or just plain HTTP-style headers dumped into a file. If the name is an empty string, no cookies are loaded, but cookie handling is still enabled.
This means that the CURLOPT_COOKIEFILE needs to refer to a file which contains the cookies you need to send. The file is a file which exists on the server running cURL, which, in turn, will transmit the cookie data to the remote host. I'd expect read access to be required on the file.
A good answer describing how the Netscape format looks like can be found here
In addition, if you want to keep the cookies that are generated during the request, you also need to set a CURLOPT_COOKIEJAR.
I am attempting to send a PUT request using PHP and curl and my query parameters do not seem to be making it to the API. The logs verify that the request is coming in as a PUT but the parameters are not making it. I've followed every example I could find on the internet that describes how to build the query parameters building it manually and using the http_build_query function. I then add the parameters using the CURLOPT_POSTFIELDS value. Am I missing something needed for query params with a PUT request?
Unlike the POST request, the PUT request requires to specify the Content-Length that you are going to send to the server. Here is an example:
// data to be sent
$post_data = "...soem data";
// so that you get response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// this is must for doing PUT
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($post_data)));
// Doing PUT
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data );
If you still have problem after this, run the curl by enabling the verbose mode and it will show you the debug msg on curl's operation:
curl_setopt($ch, CURLOPT_VERBOSE, true);
The problem the following code piece successfully sends POST request but doesn't send the data in $sendStream (the stream is valid and contains data - this has been verified):
curl_setopt($request, CURLOPT_HTTPHEADER, array('Content-type: application/x-rethync-request'));
curl_setopt($request, CURLOPT_HEADER, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_INFILE, $sendStream);
curl_setopt($request, CURLOPT_INFILESIZE, stream_length($sendStream));
curl_setopt($request, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($request);
I've read probably all cURL POST-related posts here on SO, but no luck. Why is the data not posted?
The missing piece was
curl_setopt($request, CURLOPT_UPLOAD, 1);
The PHP documentation mentions this option briefly and nothing suggests that the option should be set. Still at least some (if not all) versions of cURL don't post data despite it's specified via CURLOPT_INFILE, unless CURLOPT_UPLOAD is also set.
NOTE: when you use this method to send the data, the response header will contain HTTP/1.1 100 Continue first, followed by HTTP/1.1 200 OK and so on. So when you parse response headers, beware of the first 100 Continue response (you'll need to strip it).