File Name: sms1.php
Located at: http://techmentry.com/sms1.php
PHP code of sms1.php:
<?php
//Variables to POST
$user = "hidden";
$password = "hidden";
$mobiles = "$_POST[phone]";
$message = "$_POST[msg]";
$sender = "$_POST[sender]";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hidden/sendhttp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"user=$user&
password=$password&
mobiles=$mobiles&
message=$message&
sender=$sender"
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
?>
<br><br><br>
<form name='sms' action='' method='post'>
Phone number<br/><input type='text' name='phone' value='' maxlength=12/>
<br/>
Sender ID (from) <br/><input type='text' name='sender' value='' maxlength=15/>
<br/>
Message : <br/><textarea rows=5 cols=30 name='msg'></textarea>
<br/>
<input type='submit' value='Send'>
</form>
Please see the output on http://techmentry.com/sms1.php . It is already displaying an error code (105) (already - because it should show the error code when user click send button). 105 error code means missing 'password' parameter. But I have already stated the password in the code.
Please help :)
You're not checking whether the form was submitted, so you're running the cURL code even when displaying the initial form. You need to do:
Change the submit button to:
<input type='submit' name='submit' value='Send'>
and change the PHP code at the top to:
<?php
if (isset($_POST['submit'])) {
//Variables to POST
$user = "hidden";
$password = "hidden";
$mobiles = $_POST['phone'];
$message = $_POST['msg'];
$sender = $_POST['sender'];
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hidden/sendhttp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('user' => $user,
'password' => $password,
'mobiles' => $mobiles,
'message' => $message,
'sender' => $sender)
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
The other change I made was to use an array instead of a string as the parameter for CURLOPT_POSTFIELDS. This avoids having to urlencode() all the parameters, which you weren't doing.
And the proper way to assign variables from an associative array is like:
$mobiles = $_POST['phone'];
The key should be in quotes, the array name itself should not. Your way worked because of the way variable interpolation in strings works, but it's not generally done that way unless you're embedding the variable in a longer string, like:
echo "The phone number is $_POST[phone]";
Many programmers avoid this syntax entirely, preferring concatenation:
echo "The phone number is ". $_POST['phone'];
That's a stylistic choice, there's no widespread concensus either way.
"Using PHP variables and newlines within strings often results in problems, could you try the following:
if ($_SERVER['REQUEST_METHOD'] == "POST")){
//this prevents code being executed on page load
$user = "hidden";
$password = "hidden";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.hidden/sendhttp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
//removed newlines and variables in string
curl_setopt($ch, CURLOPT_POSTFIELDS,"user=".$user.
"&password=".$password.
"&mobiles=". $_POST['phone'].
"&message=". $_POST['msg'] .
"&sender=".$_POST['sender']);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
<br><br><br>
<form name="sms" action="" method="post">
Phone number<br/><input type="text" name="phone" value="" maxlength="12"/>
<br/>
Sender ID (from) <br/><input type="text" name="sender" value="" maxlength="15"/>
<br/>
Message : <br/><textarea rows="5" cols="30" name="msg"></textarea>
<br/>
<input type="submit" value="Send">
</form>
Qualifier: I'm learning PHP.
Shouldn't you wrap your operations in if(isset($_POST['submit']))?
It is processing before you submit anything.
Related
I have a simple form:
<form action="" method="POST">
<input type="text" name="arg1"/>
<input type="text" name="arg2"/>
<input type="submit"/>
</form>
Then when it's submitted, I POST arg1 via cURL and update its value based on the cURL response data. I then call another function to change arg2 using a call to a Java program with exec.
And these two updated arg1 and arg2 should be the value attributes for that form after I submit it initially. (So that after the first submit, I just need to click "submit" as long as I keep getting data from the cURL request)
I tried using global as seen in this post: Giving my function access to outside variable ,but it doesn't work for me
My code is something like that:
<form action="" method="POST">
<input type="text" name="arg1" value="<?php echo $arg1;?>"/>
<input type="text" name="arg2" value="<?php echo $arg2;?>"/>
<input type="submit"/>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$arg1_post = $_POST["arg1"];
$arg2_post = $_POST["arg2"];
$curl_post_fields = '{"arg1":' . $arg1_post . ', "arg2": "'. $arg2_post . '"}';
sendCurlPost($curl_post_fields);
}
function sendCurlPost($curl_post_fields){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url/" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
if ($response=curl_exec($ch)){
global arg1;
$arg1 = // some value from the $response
$output=null;
$retval=null;
runJavaProgram();
}
}
function runJavaProgram (){
exec('C:\Java\jdk-18\bin\java.exe Main.java 2>&1 ', $output, $retval);
global $arg2;
$arg2 = $output[0];
I keep getting Undefined variable even after the first POST. I also tried other methods using SESSION and COOKIE but that didn't work either
This should be a pretty simple questions but I can't seam to find a simple answer. All of the questions I find deal with same jquery.
I have a php page that accepts for post data, places it in an array, passes the array to an api, and receives success/error from api.
I have an html page with a form. When I submit the form it passes the form data to the php file.
All I would like to do is return the success/error message's variable back to the html file. I don't care if the page reloads, I don't want any fancy features I'm just trying to do a simple test but have forgotten my php 101. any help or direction to references would be appreciated.
Html:
<div style="width: 400px; margin: 150px auto;">
<form action="api3.php" method="post">
<input type="text" placeholder="First Name" name="fname"><br><br>
<input type="text" placeholder="Last Name" name="lname"><br><br>
<input type="email" placeholder="Email" name="email"><br><br>
<input type="text" placeholder="Phone" name="phone"><br><br>
<select name="life"><br><br>
<option value="customer">Customer</option>
<option value="lead">Lead</option>
<option value="subscriber">Subsciber</option>
<option value="opportunity">Opportunity</option>
</select><br><br>
<input type="text" placeholder="Pizza" name="pizza"><br><br>
<input type="submit" value="Submit">
</form>
</div>
PHP:
<?php
$arr = array(
'properties' => array (
array(
'property' => 'email',
'value' => $_POST["email"]
),
array(
'property' => 'firstname',
'value' => $_POST["fname"]
),
array(
'property' => 'lastname',
'value' => $_POST["lname"]
),
array(
'property' => 'phone',
'value' => $_POST["phone"]
),
array(
"property" => "lifecyclestage",
"value" => $_POST["life"]
),
array(
"property" => "pizza",
"value" => $_POST["pizza"]
)
)
);
$post_json = json_encode($arr);
$hapikey = "/";
$endpoint1 = 'http://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/' . $arr['properties'][0]['value'] . '/?hapikey=' . $hapikey;
$endpoint2 = 'http://api.hubapi.com/contacts/v1/lists/5/add?hapikey=' . $hapikey;
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
#curl_setopt($ch, CURLOPT_URL, $endpoint1);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response1 = #curl_exec($ch);
$status_code1 = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors1 = curl_error($ch);
if ($status_code1 == 200) {
$vid = json_decode($response1, true);
echo $vid['vid'] . '<br><br><br>';
$arr2 = array(
'vids' => array (
$vid['vid']
)
);
$vids_push = json_encode($arr2);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $vids_push);
#curl_setopt($ch, CURLOPT_URL, $endpoint2);
$response2 = #curl_exec($ch);
$status_code2 = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors2 = curl_error($ch);
#curl_close($ch);
return $response2;
}
?>
EDIT: I changed my form.html page to .php. I didn't want to share my code because it always seams to complicate things but all I want is to return $response2 back to my form.php page.
First of all the page you have form and want to get response should be with .php
Now for example, I have a page with form at www.example.com/work.php
//Your form here
<form> </form>
submit the form on other .php page that process input and get response from API.
at the end of page you have two methods to return data.
using GET
encode your variables in url and redirect page to work.php
$url = "www.example.com/work.php" + "?status=error&message=This is message";
header('Location: '.$url);
Now on work.php file you need to utilize these parameters we encoded with url using
echo $_GET['status'];
echo $_GET['message'];
// rest of the page will be same.
using SESSION
store variables in session and redirect to work.php without parameters
$_SESSION['status'] = "error";
$_SESSION['message'] = "This is message";
$url = "www.example.com/work.php";
header('Location: '.$url);
Again in work.php file display data from session and rest of code will be same.
I want to get response from the URL http://webcache.gmc-uk.org/gmclrmp_enu/start.swe by sending some POST parameters into it.
I have written a function as follows:
public function getResponse($params, $url)
{
$post_data = '';
foreach ($params as $key => $value) {
$post_data .= $key . '=' . $value . '&';
}
//create the final string to be posted using implode()
$post_data = rtrim($post_data, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($post_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
But, when I call this function as:
public function getIndex()
{
$params = array(
"s_3_1_5_0" => "5205500"
);
$url = 'http://webcache.gmc-uk.org/gmclrmp_enu/start.swe';
echo $this->getResponse($params, $url);
}
I am always redirected to the page: http://localhost:8000/gmclrmp_enu/start.swe
instead of getting response from the specified URL.
where, http:// localhost:8000/ is root URL of my local project.
How could I get the response from that URL and dump it?
I tried your code and got this response.
<html>
<body>
<form action="/gmclrmp_enu/start.swe" method="POST" name="RedirectForHost">
<input type = "hidden" name="s_3_1_5_0" value="5205500">
<input type = "hidden" name="SWEBHWND" value="">
<input type = "hidden" name="_sn" value="QG3N3byeQ-lw2X0470Taoo2Mr1xU6fklSaK4bX.yXOH1DqcEpmRHVzctRxa1UYflcu-svwV7M0VFB6KvWmUEh4mUDGbJtaXeil1PnikFKVpr6fRP.i-GMhMRm41kZVFHaZA1QBjteOlfTXcwF0CLSh.MzwHUhdPVvYK9Ulfe.zCJQiSkU2XOt68YjT1lD-4jrTrIBzxJLUY_">
<input type = "hidden" name="SRN" value="">
<input type = "hidden" name="SWEHo" value="">
<input type = "hidden" name="SWETS" value="1481276787">
</form>
<script language="javascript">
var formObj = document.forms["RedirectForHost"];
formObj.SWEHo.value=top.location.hostname;
formObj.submit();
</script>
</body>
</html>
I think you got response like this, and then javascript submitted the form to localhost:8000.
i send data to server from pc local through this form :
<form method="post" action="proses.php">
<input type="hidden" name="id"><br>
Tanggal <input type="text" name="tgl"><br>
Pesan <textarea name="isi" cols="29" rows="5"></textarea> <br>
Nomor Hp <input type="text" name="nope"><br>
<input type="submit" name="submit" value="Submit"> </form>
and this is code proses.php
<?php
$id = $_POST['id'];
$tgl = $_POST['tgl'];
$isi = $_POST['isi']; $nope= $_POST['nope'];
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, 'http://adibiken.com/SEM/kir.php');
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, "id=".$id."&tgl=".$tgl."&isi=".$isi."&nope=".$nope);
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_TIMEOUT,30);
curl_setopt($curlHandle, CURLOPT_POST, 1);
curl_exec($curlHandle);
curl_close($curlHandle); ?>
this is code for kir.php ( in server )
<?php $id = $_POST['id']; $tgl = $_POST['tgl']; $isi = $_POST['isi']; $nope = $_POST['nope'];
$quer = "INSERT INTO `adibiken_sem`.`inbox` (`id`,`tgl` ,`isi` ,`nope`)VALUES ( '$id','$tgl', '$isi', '$nope')";
mysql_query($quer); ?>
PROBLEM : data are have been successful SENDING with EMPTY RECORD IN DATBASE SERVER...
need help please
Don't know if this is the problem but for sure
VALUES ( '$id','$tgl', '$isi', '$nope')"
should be
VALUES ( '".$id."','".$tgl."', '".$isi."', '".$nope."')"
In file "proses.php" try using "&" instead of "&". Also in file kir.php ( in server ) run print_r($_POST) or log the whole array somewhere to chceck if you are recieving the correct variable names.
use the http_build_query(...) to form postfields from array.
$request = http_build_query($_POST);
...
curl_setopt($curlHandle, CURLOPT_POSTFIELDS,$request)
If you getting correct values in $_POST array then something is wrong with your SQL syntax. You can try to execute your query in phpMyAdmin or something else and debug it.
It might happen that MySQL fires an error, but you could not see it. You can get any output from kir.php including error messages (it is enabled on your server):
$curResponse = curl_exec($curlHandle);
echo $curResponse;
i have no idea how to solve a problem with sending $_POST. I want to fill a form at example.com
//at example.com
<form action="foo.php" method="post" >
<input name="bar1" type="text" />
<input name="bar2" type="text" />
<input name="bar3" type="text" />
<input value="Send" type="submit" />
</form>
and then it goes to foo.php :
<?php //foo.php
echo 'added: <p>'.$_POST['bar1'].'<br />'.$_POST['bar2'].'<br />'.$_POST['bar3'];
?>
and in the same time it also send
$_POST['bar1'], $_POST['bar2'], $_POST['bar3']
to exampledomain.com/foobar.php where it can be saved to a file - that's not a problem.
I don't know how to send info to both php scripts at once - one is external one. I guess i have to send it somehow inside foo.php
There is kind of solution - redirecting to exampledomain.com/foobar.php inside foo.php but it isn't acceptable in my case - I want to do it without making user exit example.com
Thanks in advance and hope you can undestand my problem - if not just ask a comment
EDIT: Based on Pete Herbert Penito's answer:
<?php //inside foo.php
$url = 'http://exampledomain.com/foobar.php';
$fields_string='';
foreach($_POST as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
I would use CURL to construct a post request:
<?php
// these variables would need to be changed to be your variables
// alternatively you could send the entire post constructed using a foreach
if(isset($_POST['Name'])) $Name = $_POST['Name'];
if(isset($_POST['Email'])) $Email = $_POST['Email'];
if(isset($_POST['Message'])) $Message= htmlentities($_POST['Message']);
$Curl_Session = curl_init('http://www.site.com/cgi-bin/waiting.php');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "Name=$Name&Email=$Email&Message=$Message");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_exec ($Curl_Session);
curl_close ($Curl_Session);
?>
From Link:
http://www.askapache.com/php/sending-post-form-data-php-curl.html
In your foo.php:
<?php
include 'http://exampledomain.com/foobar.php';
Note: you need to enable allow_url_fopen in your php.ini file.
You will need to do one of the POST's with javascript ajax.
Then the real post which will redirect the browser like normal.
http://www.w3schools.com/jquery/ajax_post.asp
$(selector).post(url,data,success(response,status,xhr),dataType)