I'm trying to update a mySQL database with cURL and PHP, but the values that are supposed to be sent by the post aren't inserted into the database. I don't get any errors and cURL is enabled according to phpinfo(). Here is my script:
<?php
$data = [
'email' => 'jsnow#got.com',
'token' => '58938539'
];
$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
And here's the receiver script:
<?php
require 'includes/db.php'; // Database connection. Connects with no errors raised
if(isset($_POST['email']) AND isset($_POST['token'])) {
$email = $_POST['email'];
$token = $_POST['token'];
$q = "INSERT INTO reset_tokens(email, token)";
$q .= " VALUES(?, ?)";
$stmt = $pdo->prepare($q);
$test = $stmt->execute([$email, $token]);
}
?>
Looks like you are missing HTTP Auth for http://www.econcentre.com/receiver.php.
You should include the HTTP Auth in the cURL request:
<?php
$data = [
'email' => 'jsnow#got.com',
'token' => '58938539'
];
$username = "auth-user";
$password = "auth-pwd";
$string = http_build_query($data);
$ch = curl_init("http://www.econcentre.com/receiver.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);
?>
Related
I'm writing a script to add a new user to the hosting control panel "VESTA". I write according to the manual for api: http://vestacp.com/docs/api/ Errors when executing the code does not produce, but the user is not added to the VESTA. My code:
$vst_hostname = 'dfsdfl.csfdsdsgds.com';
$postvars = array(
$vst_username = 'zdeslogin',
$vst_password = 'zdesparol',
$vst_returncode = 'yes',
$vst_command = 'v-add-user',
$username = 'demo',
$password = 'password',
$email = 'demo#gmail.com',
$package = 'default',
$fist_name = 'Rust',
$last_name = 'Cohle'
);
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
echo "User account has been successfuly created\n";
} else {
echo "Query returned error code: " .$answer. "\n";
}
After running the code, the following line is returned: User account has been successfuly created.
The Russian community helped. Here is the code:
<?php
// Server credentials
$vst_hostname = 'sadsad.sadsad.com';
$vst_username = 'zdeslogin';
$vst_password = 'zdesparol';
$vst_returncode = 'no';
$vst_command = 'v-add-user';
$username = 'demo3';
$password = 'd3m0p4ssw0rd';
$email = 'demo#gmail.com';
$package = 'default';
$fist_name = 'Rust';
$last_name = 'Cohle';
$postvars = array(
'user' => $vst_username,
'password' => $vst_password,
'returncode' => $vst_returncode,
'cmd' => $vst_command,
'arg1' => $username,
'arg2' => $password,
'arg3' => $email,
'arg4' => $package,
'arg5' => $fist_name,
'arg6' => $last_name
);
$postdata = http_build_query($postvars);
// Send POST query via cURL
$postdata = http_build_query($postvars);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://' . $vst_hostname . ':8083/api/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
$answer = curl_exec($curl);
// Check result
if($answer == 0) {
echo "User account has been successfuly created\n";
} else {
echo "Query returned error code: " .$answer. "\n";
}
?>
I have two scripts:
First to send:
$url = "http://localhost/curl2.php";
$data = array('email' => 'test#example.com');
$addr = $url . '?' . http_build_query($data);
$ch = curl_init($addr);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_exec($ch);
And second to receive:
if($_SERVER['REQUEST_METHOD'] == "PUT") {
$data = array();
$incoming = file_get_contents("php://input");
parse_str($incoming, $data);
echo "Address: " . filter_var($data["email"], FILTER_VALIDATE_EMAIL);
}
But variable $incoming is empty. How can I do it? Maybe it has something to do with PUT, but I must use PUT.
PUT is not GET. Use the URL without a query string and add the data to CURLOPT_POSTFIELDS:
$url = "http://localhost/curl2.php";
$data = array('email' => 'test#example.com');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_exec($ch);
In the following custom function...
function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
echo $balance['balance']['sms'];
}
I return a balance from a text local account. Now i try and echo this balance into place...
$balance = get_txtlocal_balance();
$title = 'SMS Integration - Current Balance: ' . $balance;
<span><?php echo $title;?></span>
But the returned value doesnt appear in the right place, it always returns are the top of the page, can anyone spot what im doing wrong?
Replace Below Code :
function get_txtlocal_balance() {
$username = variable_get('sms_txtlocal_email');
$hash = variable_get('sms_txtlocal_password');
$data = array('username' => $username, 'hash' => $hash);
$ch = curl_init('http://api.txtlocal.com/balance/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$balance = json_decode($response, true);
return $balance['balance']['sms'];
}
Im trying my hand at using curl to post some data, I am not reciving my post content and can not find the source of the problem
curl.php
<?php
$data = array("user_email" => "22" , "pass" => "22" );
$string = http_build_query($data);
$ch = curl_init("http://localhost:8888/290_project/test.php"); //this is where post data goes too, also starts curl
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch) //ends curl
?>
test.php
<?php
if(isset($_POST['user_email'], $_POST['pass'])) {
$name = $_POST['user_email'];
$pass = $_POST['pass'];
echo $name;
echo $pass;
} else {
echo "error";
} ?>
Every time I get my error response meaning the post data is not going through. I have tried everything I could think of to trouble shoot;I must be over looking something I am simply not yet familiar with?
Please set CURLOPT_URL to http://localhost:8888.....
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:8888/290_project/test.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
if(!curl_errno($ch)){
$info = curl_getinfo($ch);
} else {
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch) //ends curl
?>
With curl_getinfo() - Gets information about the last transfer.
For more detail read below link:- http://php.net/manual/en/function.curl-getinfo.php
I have edited the answer, The reason for error is not curl.
http_build_query($data, '', '&');
Following is working example. Please try this.
$postData = array(
'user_name' => 'abcd',
'password' => 'asdfghj',
'redirect' => 'yes',
'user_login' => '1'
);
$url='http://localhost:8888/290_project/test.php';
$ch = curl_init();
//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $url);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute the request
$store = curl_exec($ch);
print_r($store);
curl_close($ch)
You have an error in your php code:
edit:
if(isset($_POST['user_email'], $_POST['pass'])) {
to:
if(isset($_POST['user_email']) && isset($_POST['pass'])) {
My PHP code (free.php) on http://techmentry.com/free.php is
<?php
{
//Variables to POST
$access_token = "b34480a685e7d638d9ee3e53cXXXXX";
$message = "hi";
$send_to = "existing_contacts";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('access_token' => $access_token,
'message' => urlencode('hi'),
'send_to' => $send_to,)
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
I am getting this error: THE MESSAGE WAS BLANK
This error means: "The message field was blank or was not properly URL encoded" (as told by my SMS gateway). But as you can see that my message field isn't blank.
I believe you can't send an array to CURLOPT_POSTFIELDS, you would need to replace the line with the following
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$accesstoken."&message=".urlencode('hi')."&send_to=".$send_to);
I hope this solves it
Use http_build_query():
<?php
{
$postdata = array();
//Variables to POST
$postdata['access_token'] = "b34480a685e7d638d9ee3e53cXXXXX";
$postdata['message'] = "hi";
$postdata['send_to'] = "existing_contacts";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata) );
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>