Passing $_POST values with cURL
Should work fine.
$data = array('name' => 'Ross', 'php_master' => true);
// You can POST a file by prefixing with an @ (for <input type="file"> fields)
$data['file'] = '@/home/user/world.jpg';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
curl_close($handle)
We have two options here, CURLOPT_POST
which turns HTTP POST on, and CURLOPT_POSTFIELDS
which contains an array of our post data to submit. This can be used to submit data to POST
<form>
s.
It is important to note that curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
takes the $data in two formats, and that this determines how the post data will be encoded.
$data
as anarray()
: The data will be sent asmultipart/form-data
which is not always accepted by the server.$data = array('name' => 'Ross', 'php_master' => true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);$data
as url encoded string: The data will be sent asapplication/x-www-form-urlencoded
, which is the default encoding for submitted html form data.$data = array('name' => 'Ross', 'php_master' => true);
curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($data));
I hope this will help others save their time.
See:
curl_init
curl_setopt
CURL + Passing $_POST variables/array
In my opinion the best way (secure) method for passing username and password as HTTP HEADER AUTHENTICATION WITH CURL.
if(isset($_POST['thisvalue'])) {
$username = $_POST['username'];
$passwd = $_POST['passwd'];
$data = array('serialno' => '12345');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.domain.com/scripts/curl/index_general.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_USERPWD,'username:password'); // set your username and password
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_REFERER,'http://www.domain.com.au');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
In index_general.php
you can access the http username and password as
$_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW']
Passing GET variables in a PHP CURL POST request
It seems that you are doing a GET request, so the post variables will never be send to the server. You could just add the parameters as a query string to the url.
Like so:
$curl_connection = curl_init('http://10.219.5.109:9000/mean?sample=1000');
$result = curl_exec($curl_connection);
print_r(curl_getinfo($curl_connection));
If you want the data to be dynamic, you can do it like this:
$post_string = 'sample=1000';
$curl_connection = curl_init('http://10.219.5.109:9000/mean?' . $post_string);
PHP, cURL, and HTTP POST example?
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>
Passing form variable into php curl request
Try to json_encode
your params
$params = [
"method"=>"liststreamitems",
"params"=>["Device"],
"chain_name"=>$ChainName
];
Then pass it to the option CURLOPT_POSTFIELDS
like this
CURLOPT_POSTFIELDS => json_encode($params);
can't pass the post value to curl
Just use PHP's built-in curl commands:
<?php
if(isset($_POST['conversation'])) {
$data = array("input"=>array("text"=>$_POST["conversation"]));
$url = "https://gateway.watsonplatform.net/conversation/api/v1/workspaces/a9379972-d820-4cdf-b1cb-ad0af898a534/message?version=2016-07-11";
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_USERPWD => "username:password",
CURLOPT_HTTPHEADER => array("Content-Type:application/json"),
CURLOPT_POSTFIELDS => json_encode($data),
));
$response = curl_exec($ch);
curl_close($ch);
print_r(json_decode($response));
}
?>
How do I submit POST data using PHP and cURL?
function post($requestJson) {
$postUrl = $this->endpoint."?access_token=".$this->token;
//Get length of post
$postlength = strlen($requestJson);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$postUrl);
curl_setopt($ch,CURLOPT_POST,$postlength);
curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
//close connection
curl_close($ch);
return $response;
}
Related Topics
Replacing Accented Characters PHP
How to Check If Mod_Rewrite Is Enabled in PHP
Force File Download With PHP Using Header()
How to Stop People Hacking the PHP-Based Highscore Table of a Flash Game
Getting Checkbox Values on Submit
"The Page Has Expired Due to Inactivity" - Laravel 5.5
Pdo Binding Values For MySQL in Statement
Does $_Server['Http_X_Requested_With'] Exist in PHP or Not
Pdo::_Construct(): Server Sent Charset (255) Unknown to the Client. Please, Report to the Developers
PHPexcel Runs Out of 256, 512 and Also 1024Mb of Ram
How to Enable Curl in PHP/Xampp
PHP Exec Command (Or Similar) to Not Wait For Result
How Create an Array from the Output of an Array Printed With Print_R
How to Fix Error: Laravel.Log Could Not Be Opened
Getting the Names of All Files in a Directory With PHP
PHP.Ini & Smtp= - How to Pass Username & Password