About a couple of months ago I could upload photos from my bot path with 5.5 version. Now I upgraded my PHP to 5.6 and I don't know why but I can't anymore.
This was my code:
$url = "https://api.telegram.org/bot".Token."/sendPhoto?chat_id=".$chat_id;
$post_fields = array(
'photo' => new CURLFile(realpath("test.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);
Use this function i wrote for CURL :
function makeHTTPRequest($method, $types = []){
$url = 'https://api.telegram.org/bot'.Token.'/'.$method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($types));
$res = curl_exec($ch);
if (curl_error($ch)){
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}
Then call it wherever you want :
var_dump(makeHTTPRequest('sendPhoto', [
'chat_id' => $chat_id,
'photo' => new CURLFile('test.png')
]));
Related
I want to change profile image another application curl request i am trying below code can anyone help me please
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}
As Anoxy said, you need to put in the header the Content-Type :
$viewer = Engine_Api::_()->user()->getViewer();
$apiData = array(
"email" => $viewer->email,
"profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";
$response = $this->callRiseAPI2($apiData,$apiHost);
private function callRiseAPI2($apiData,$apiHost){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, count($apiData));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: multipart/form-data');
$jsonData = curl_exec($ch);
if (false === $jsonData) {
throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
}
curl_close($ch);
//return the API response
return json_decode($jsonData);
}
I'm trying to call this
[Route("CreateHL7TestFile")]
[HttpPost]
public IActionResult CreateHL7TestFile([FromBody] JObject data)
{
// Code here
}
from a php function using cUrl. When I try and call the API without passing any data it works fine. It's when I try and add the '$data' that I cannot hit the API. When I call the API with data from Postman it also works fine. So I'm assuming my error is on the php side?
My php function looks like this.
public function CallAPI($method, $url, $data = false)
{
$data = array("value1" => "some value");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('test1' => 'value1', 'test2' => 'value2')));
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
http://localhost:50999/api/ParticipantAPI/CreateHL7TestFile
Any help would be much appreciated.
Thanks,
Adam
Use a proper model on the asp.net-core server to bind incoming data
public class MyData {
public string value1 { get; set;}
}
And update action accordingly
[Route("CreateHL7TestFile")]
[HttpPost]
public IActionResult CreateHL7TestFile([FromBody] MyData data) {
// Code here
}
Referencing the following article POSTing JSON Data With PHP cURL
Try the following on the PHP side
$data = array("value1" => "some value");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
For your real requests refactor the expected data model accordingly.
Try something like this:
public function CallAPI($method, $url, $data = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('test1' => 'value1', 'test2' => 'value2')));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
http://php.net/manual/en/function.http-build-query.php
If you want to send json, then I think you'll have to set the header, something like:
public function CallAPI($method, $url, $data = false)
{
$dat = array("TEST1","TEST2");
$data = json_encode($dat);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
<?php
$UploadURL = $CreatFile->upload->posturl;
$file = $_SERVER['DOCUMENT_ROOT'] . '/picasa/preview_alpha.jpg';
$post = array(
'filename' => '#'.$file
);
$header = array(
"Content-Length: ".filesize($file),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $UploadURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$hasil = curl_exec($ch);
$hasil1 = curl_error($ch);
curl_close($ch);
return $hasil.$hasil1;
Result:
computer says yes
1
but in the share file, the file didn't upload
if use puturl then it can't conect to server
https://open.ge.tt/1/doc/rest#files/{sharename}/create
Currently, to POST image from local folder I use curl and PHP, here is my code:
$url = 'http://myprestashop.com/api/images/products/1';
$image_path = 'C:\\my_image.png';
$key = 'My web service key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '#'.$image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
How can I POST image from URL (http://imageserver.com/image.jpeg) instead of local folder (C:\my_image.png)? Is it possible?
Just change your image path and try this code.
$url = 'http://myprestashop.com/api/images/products/1';
$data = array('key' => 'My web service key', 'image_path' => '#/home/user/test.png');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
i want send a post HTTPS request with php ,similar this request in python:
API_HOST = "android.clients.google.com"
API_PAGE = "/market/api/ApiRequest"
parmas=params = urllib.urlencode({
"version" : 2,
"request" : request
})
headers = {"Content-type": "application/x-www-form-urlencoded"}
connection = httplib.HTTPSConnection( API_HOST )
connection.request( "POST", API_PAGE, params, headers )
resp = connection.getresponse().read()
connection->close()
I tried to this lib
https://github.com/dcramer/php-httplib
but this lib , not support HTTPS
my php code:
$query_data = array(
"version" => 2,
"request" => $request
);
$params = http_build_query($query_data);
$headers = array("content-type"=> "application/x-www-form-urlencoded");
$API_URL = $API_HOST.$API_PAGE;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$resp = curl_exec($ch);
python code works, but php code return Bad Request, Error 400
Can you try this:
<?php
// Data
$postfields = array(
'version' => 2,
'request' => $request
);
// Post It
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://android.clients.google.com/market/api/ApiRequest');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
// Debug
var_dump($result);