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>
Related
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>
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>
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);
?>
I am trying to pass data from an HTML form POST using curl, does anyone know how to do this?
My code: (dtda.php)
<?php
// HTTP authentication
$url = "http://siteapi.com.br:9988/api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "Teste:123456");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
My second code HTML: (sending.html)
<form name="myform" id="myform" action="http://endereco.com.br/dtda.php" method="post" >
<input type="text" name="op" value="event"/>
<input type="text" name="seq" value="45" />
<input type="text" name="type" value="nexttrack" />
<input type="text" name="priority" value="1" />
<input type="submit" style="padding-left:10px; padding-right:10px" class="button compact" onclick="javascript:openlive()" value="Ativar agendamento" /><p></form> <br /><br />
</form>
As I pass the form data to the "endereco.com.br/dtda.php" ?
try
$url = "http://siteapi.com.br:9988/api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(array('yourData' => $data)));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
If you want to forward the posted data to another URL, you can add it like this:
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input'));
is the first time that I compile with a curl: i'm trying to use it to send post values with header. i did many tests, but probably I mistake something because I in output don't see my post vars.
form page:
<html>
<form action="analize.php" method="post">
<input type="text" name="people" value="" />
<input type="submit" value="Send" />
</form>
</html>
analize.php:
<?php
$url = 'test.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_exec($ch);
curl_close($ch);
header('Location: ' . $url);
exit;
?>
test.php:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'analize.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_exec($ch);
$post = curl_getinfo($ch);
curl_close($ch);
var_dump($post);
?>