I want to convert a post method form to curl command - php

I'm making an auto SMS system. But SMS provider gave me post method form but I want it to modify with curl command by my own.
they provided me
<head>
<title>SMS</title>
</head>
<body>
<form name="sms" method="post" >
<input type="text" placeholder="send to number" name="to" /><br />
<input type="textarea" placeholder="sms" name="msg" /><br />
<input type="submit" value=" OK " />
</form>
<?php
$apikey='$2y$10$t..Yr.YDG0kXYiuLwQ78OecDgz6/qh.1xSWx77mXjczkk3AEKvTZe';
if (isset($_POST["msg"]) ) {
$sendto = $_POST["to"];
$fullNumber = '880' . substr(preg_replace('/\D/', '', $sendto), -10);
$msg = urlencode($_POST["msg"]);
// $masking='CITY PORTER';
// $masking=urlencode($masking);
// masking $url='http://example.com/smsapi/masking?api_key='.$apikey.'&smsType=text&maskingID='.$masking.'&mobileNo='.$fullNumber.'&smsContent='.$msg.'';
$url='http://example.com/smsapi/non-masking?api_key='.$apikey.'&smsType=text&mobileNo='.$fullNumber.'&smsContent='.$msg.'';
if ( !empty($_POST["to"]) && !empty($_POST["msg"])) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL =>$url,
CURLOPT_USERAGENT =>'My Browser'
));
$resp = curl_exec($curl);
echo $resp;
curl_close($curl);
}
else
{ echo "Field is empty";}
}
?>
</body>
I converted it to curl command but this is not still working. I just want to input multiple value without is set() 1 value.
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row[mailing_no];
$message = "Dear Guardian, ". $row[name] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);

Try this i hope your problem will be solve
<div id="send_reply"></div>
<form name="sms" method="post" >
<input type="text" placeholder="send to number" name="to" id="phone" /><br />
<input type="textarea" placeholder="sms" name="msg" id="mess" /><br />
<input type="submit" value=" OK " id="send_btn" />
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#send_btn").click(function(){
var txt1=$("#phone").val();
var txt2=$("#mess").val();
$.ajax({
url : 'check.php',
method : 'GET',
data : {txt1:txt1,txt2:txt2},
success:function(dda){
$("#send_reply").html(dda);
}
});
});
});
</script>

I think we were missing double quotes for $row keys, try this:
$api_key = "$2y$10$mtW.yfKj18i2mTPe/0iCEuKdCfCGh9zOYYEU9AmnMrJyBb.h7fVcG";
$number = $row["mailing_no"];
$message = "Dear Guardian, ". $row["name"] . " has swiped his card right now";
$type= "text";
$params = array('api_key'=>$api_key, 'smsType'=>$type, 'mobileNo'=>$number, 'smsContent'=>$message);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/smsapi/non-masking?".http_build_query($params, "", "&"));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close ($ch);

Related

How to pass a variable from an html form to a php script?

I have watched some tutorials on how to pass a variable from a form to a php file, and cannot get anything to happen when I click the submit button. I am making a page that searches the Clash of Clans API to display the clan searched.
I just need to have the form submit the clan tag to the php script.
Here is my code. The file is a php file and the form action is blank because the php is in the file.
<?php
header('Content-Type: text/html; charset=UTF-8');
if (isset($_POST['submit'])) {
$clantag = $_POST['var'];
}
$token = Token Goes Here;
$url = "https://api.clashofclans.com/v1/clans/" . urlencode($clantag);
$ch = curl_init($url);
$headr = array();
$headr[] = "Accept: application/json";
$headr[] = "Authorization: Bearer ".$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
$data = json_decode($res, true);
curl_close($ch);
echo "<pre>";
var_dump($data);
echo "</pre>";
?>
<form action="" method="post">
<input id="clan-tag" type="text" name="var" value="" />
<input id="clanSearchBtn" type="button" name="submit" value="Submit">
</form>
You should replace the type of the button from "button" to "submit"
<form action="" method="post">
<input id="clan-tag" type="text" name="var" value="" />
<input id="clanSearchBtn" type="submit" name="submit" value="Submit">
</form>

get response/result after submitting file to url

I'm trying to submit a file to a REST API url and need to get the response/result from this process. I'm trying to send it to another php and display the result but nothing. If it could display in the same php page then even better. How am i suppose to do that?
<?php
session_start();
// Start the session
$responseURL = "http://my-site.com/responseform.php";
$url = 'https://api-to-validate-file.com/api/validate';
$header = array('Content-Type: multipart/form-data','Authorization: apikey=0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f');
$fields = array('filename' => '#' . $_FILES['filename']['tmp_name'][0]);
$token = 'NfxoS9oGjA6MiArPtwg4aR3Cp4ygAbNA2uv6Gg4m';
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $header);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($resource, CURLOPT_POST, true);
curl_setopt($resource, CURLOPT_POSTFIELDS, $fields);
curl_setopt($resource, CURLOPT_COOKIE, 'apiToken=' . $token);
$result = json_decode(curl_exec($resource));
///echo 'The body of the response is ' . $result;
curl_close($resource);
?>
<form method="post" action="index.php" enctype="multipart/form-data">
<input name="filename" type="file" />
<input type="submit" value="Upload" />
<input type="hidden" name="RespURL" value="<?= $responseURL ?>"><br>
</form>

I am trying to make a custom User creation with ZenDesk(CRM)

This site helped me out a lot last time I had an issue. I was wondering why this php code was not working. Any help would be appreciated. I am trying to make a custom user creation request with ZenDesk.
I got the request to work, but when I edited the code to fit it into the actual Instance of my CRM it does not even try to make a PHP request. I verified this should be possible if the code is correct. I Just can not figure out why it is not correct.
<?php
include("config.php");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://url/api/v2/users.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"user\": {\"name\": \"$name\",
\"email\": \"$email\", \"phone\": \"$phone"}}");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, "email" . ":" . "password");
$phone = phone;
$email = email;
$name = name;
$headers = array();
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
if ( !isset ($_POST['Name']) ||
!isset ($_POST['Name'] ||
!isset ($_POST['phone']
)
{
}
curl_close ($ch);
?>
<!DOCTYPE html>
<html>
<body>
<form
action="://url/agent/users/{{user.id}}/requested_tickets">
First name:<br>
<input type="text" name="name" value="name">
<br>
Last name:<br>
<input type="text" name="email" value="email">
<br><br>
<input type="text" name="phone" value="+15555555555">
<br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a
page
called "/action_page.php".</p>
</body>
</html>

MailChimp group options select in php

I tried to find the answer for the last 5 hours but I finally caved in and am reaching out for help.
Basically, this code worked great until I needed to select a group option. I'm not sure if I'm creating my interests array properly, I may have to use 'merge_vars' but really would like some guidance before I spend another 5 hours blindly walking into walls.
Note for my group name I've been using the entire string "group[3117]"
Action.php
<?php
session_start();
if(isset($_POST['submit'])){
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$interest = $_POST['group[3117]'];
if(!empty($email) && !filter_var($email, FILTER_VALIDATE_EMAIL) === false){
// MailChimp API credentials
$apiKey = '+ insert api key here +';
$listID = 'insert list id';
$interest = 'insert group name'; // YOUR INTEREST/GROUP ID
// MailChimp API URL
$memberID = md5(strtolower($email));
$dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
$url = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;
// member information
$json = json_encode([
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => [
'FNAME' => $fname,
'LNAME' => $lname
],
'interests' => array(
$interest => true
),
]);
// send a HTTP POST request with curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// store the status message based on response code
if ($httpCode == 200) {
$_SESSION['msg'] = '<p style="color: #34A853">You have successfully subscribed to CodexWorld.</p>';
} else {
switch ($httpCode) {
case 214:
$msg = 'You are already subscribed.';
break;
default:
$msg = 'Some problem occurred, please try again.';
break;
}
$_SESSION['msg'] = '<p style="color: #EA4335">'.$msg.'</p>';
}
}else{
$_SESSION['msg'] = '<p style="color: #EA4335">Please enter valid email address.</p>';
}
}
// redirect to homepage
header('location:index.php');
Index.php
<?php session_start(); // place it on the top of the script ?>
<?php
$statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:'';
unset($_SESSION['msg']);
echo $statusMsg;
?>
<form method="post" action="action.php">
<p><label>First Name: </label><input type="text" name="fname" /></p>
<p><label>Last Name: </label><input type="text" name="lname" /></p>
<p><label>Email: </label><input type="text" name="email" /></p>
<p><select name="group[3117]" class="REQ_CSS" id="mce-group[3117]">
<option value=""></option>
<option value="1">Los Angeles</option>
<option value="2">Seattle</option>
<option value="4">Portland</option> </p>
<p><input type="submit" name="submit" value="SUBSCRIBE"/></p>
</form>
Here's the solution, there's a lot of manual data entry but it works, if someone has an easier method please let me know.
Enter the MailChimp API playground. Select your list / subresources / interest-categories / interest name / subresources / interests / response . scroll down and there will be a unique "id" for interest. (ignore category id)
Here is the code, credit goes Mukesh Chapegan and CodexWorld
Name this page Action.php
<?php
$email = $_POST['email'];
$memberHash = md5($email);
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$interest = $_POST['location'];
$api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // YOUR API KEY
$server = 'us14.'; //last part of ur api key
$list_id = 'XXXXXXX'; // YOUR LIST ID
$auth = base64_encode( 'user:'.$api_key );
$data = array(
'apikey' => $api_key,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
),
'interests' => array(
$interest => true
),
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$list_id.'/members/'.$memberHash);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$result_obj = json_decode($result);
echo $result_obj->status;
echo '<br>';
echo '<pre>'; print_r($result_obj); echo '</pre>';
?>
Here is the HTML form. name this index.php
Note I named the select element to location and the option values to the interest id I obtained through the API playground.
<?php session_start(); // place it on the top of the script ?>
<?php
$statusMsg = !empty($_SESSION['msg'])?$_SESSION['msg']:'';
unset($_SESSION['msg']);
echo $statusMsg;
?>
<form method="post" action="action.php">
<p><label>First Name: </label><input type="text" name="fname" /></p>
<p><label>Last Name: </label><input type="text" name="lname" /></p>
<p><label>Email: </label><input type="text" name="email" /></p>
<p> <select name="location" class="REQ_CSS" id="mce-group[3117]">
<option value=""></option>
<option value="f3eb68268d">Los Angeles</option>
<option value="3204c60199">Seattle</option>
<option value="3c5b8d638a">Portland</option> </p>
</select>
<p><input type="submit" name="submit" value="SUBSCRIBE"/></p>
</form>
If this answer helps please upvote so it's easier to find. I looked for this solution for a very very long time and found a lot of questions but few answers.

ask about send image failed using telegram API?

now i'm work with telegram API. i want send an image with this API but my code doesnt work, when i run it, i have a blank response.
here is my code :
<?php
$comment= $_POST['tag'];
$url = 'https://api.telegram.org/botMY_BOT_ID/sendPhoto';
try {
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"));
curl_setopt($curl_connection, CURLOPT_URL, $url);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, http_build_query(array('chat_id'=>'chatid','photo' => "#"."maldini.jpg")));
//curl_setopt($curl_connection, CURLOPT_INFILESIZE, filesize("path/to/maldini.jpg"));
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
//Data are stored in $data
$data = curl_exec($curl_connection);
curl_close($curl_connection);
} catch(Exception $e) {
return $e->getMessage();
}
?>
but, when i try send image with just html, i works perfectly. here is my html code :
<form method="POST" action="https://api.telegram.org/botMY_BOT_ID/sendPhoto" enctype="multipart/form-data">
<label>
<span>chat_id :</span>
<input id="chat_id" type="text" name="chat_id" value="chat_id" />
</label>
<label>
<span>caption :</span>
<input id="caption" type="text" name="caption"/>
</label>
<label>
<span>photo</span>
<input id="photo" type="file" name="photo" />
</label>
<label>
<span> </span>
<input type="submit" class="button" value="sendPhoto" />
</label>
</form>
whats wrong??? any help would be appreciate, thanks in advance :)
just need to pass chat_id with the url, try this code
<?php
$url = "https://api.telegram.org/bot<bot_id>/sendPhoto?chat_id=".$chat_id ;
$post_fields = array('chat_id' => $chat_id,'photo'=> "/path/to/image.png");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
echo "<pre>"; print_r($output);
?>

Categories