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);
?>
Related
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 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);
?>
I want to login to a site and then upload a file using curl
I have successfully logged into website, now i want to upload a file using the same curl connection. Is it possible?
My code so far:
$id ='myusername';
$password ='mypassword';
$cookie_file_path = "cookie.txt";
$agent =$_SERVER['HTTP_USER_AGENT'];
$getfile='http://archive.org/download/TestVideo_935/Backntro.mp4';
$getfile=file_get_contents($getfile);
$POSTFIELDS = array(
'username'=>$id,
'password'=>$password,
'uploadfile'=>'#'.$getfile
);
//post info for login
$LOGINURL = "http://****.com/**/;
$reffer = "http://*****.com/***/";//path to upload file
//in my case I want to upload video as this site is video site
$ch = curl_init ($LOGINURL);
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
curl_setopt($session, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data",
));
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Expect:"));
curl_setopt ($ch, CURLOPT_COOKIEFILE,$cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$info = curl_getinfo($ch);
$result = curl_exec ($ch);
print_r($info);
print_r($result);
any help will be greatly appreciated !! Thanks very much !
<form method="post" action="userfiles" enctype="multipart/form-data">
<div class="formrow">
<label for="uploadfile">File</label>
<input type="file" name="uploadfile" id="uploadfile" />
<span id="note_uploadfile"></span></div>
<div class="formrow"><label for="uploadname">
Name</label><input type="text" name="uploadname" id="uploadname" /> <span
<div class="submitrow"><label></label>
</form>
I trying to log on to my bank using curl. It all works great until it comes to the cookies (which are set by javascript). This is the form i am trying to send (the credentials are fake):
<form method="post" action="https://www.icabanken.se/Secure/Login/LoginVerify.aspx">
<input type="hidden" name="pnr" value="8502191714" />
<input type="hidden" name="JSEnabled" value="1" />
<input type="hidden" name="OBAS" value="" />
<input type="hidden" name="refurl" value="" />
<input type="hidden" name="refurlrequiressigning" value="False" />
<input type="password" class="typetext" name="Password" id="PasswordSimple" maxlength="4" value="1111" />
<input class="button-image button-small" type="submit" id="LoginSimplifiedButton" value="Logga in" />
</form>
Using following PHP / CURL code:
enter codssde here
$post_data['pnr'] = '8502191714';
$post_data['password'] = '1111';
$post_data['JSEnabled'] = '1';
$post_data['OBAS'] ='';
$post_data['refurlrequiressigning'] = '';
$post_data['refurl'] = '';
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
$post_array = implode ('&', $post_items);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_CERTINFO, 1);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 1000000);
//COOKIES
$Cookiefile='C:\wamp\www\Project1\gcookies.txt';
curl_setopt($ch, CURLOPT_COOKIEFILE, $Cookiefile);
//curl_setopt($ch, CURLOPT_COOKIEJAR, $Cookiefile);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
curl_setopt($ch, CURLOPT_URL,'https://www.icabanken.se/Secure/Login/LoginVerify.aspx');
$data = curl_exec($ch);
echo $data;
The response i get is "Your browser is not supporting cookies". It there any neat solution to get around this?
My advice would be to use something like Fiddler2:
http://www.fiddler2.com/fiddler2/
Log into your bank normally and Fiddler will log everything (You need to enable https decoding, otherwise it only see's http traffic)
From these logs, you can see the raw header information and raw cookie contents. Then use this information to replicate the login in cURL.