File_get_contents and Telegram API (reply_markup) - php

i want to make a command that send a message with button like this
So i did this
$url = "https://api.telegram.org/" . $token . "/sendMessage?chat_id=" . $message_chat_id . "&text=" . urlencode($message_text) . '&reply_markup={"inline_keyboard":[[{"text":"Visualizza spoiler!","url":"http://google.com/"}]]}';
and it make this url
https://api.telegram.org/censored/sendMessage?chat_id=censored&text=%2Fspoiler+ciao&reply_markup={"inline_keyboard":[[{"text":"Visualizza spoiler!","url":"http://google.com/"}]]}
when i use this url in browser it works but when i use file_get_contents it doesn't
someone can help me?

I had the same problem few days ago and I just tried CURL instead and it worked.
function curl_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

Related

Any ideas why this cURL script isn't working

I literally have similar cURL scripts running on other areas of this PHP page, and they work just fine. But for some reason, this one is different enough to be causing me problems. No error message, it just doesn't seem to be hitting the endpoint for some reason. Can you see any reason why this wouldn't work?
ob_start();
$destination = "https://www.example.com/record_serial_number.php?serial_number=" . $serial_number . "&employee_name=" . $employee_name . "&scan_purpose=Barcode printed&value=";
$destination = urlencode($destination);
$url = $destination;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "hidden_username:hidden_password");
$response = curl_exec($ch);
curl_close($ch);
$response .= ob_get_contents();
ob_end_clean();
You need to use curl_errno() and curl_error() to find out if curl_exec() encountered an error.

Microsoft Graph API - search on multiple email addresses?

I’m trying to query the Graph API to return me mail messages that are from OR to a particular email address by using the “participants” search term.
Docs here; https://learn.microsoft.com/en-us/graph/query-parameters
Code example (working):
$encoded_addresses = urlencode("participants:email#domain.com");
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $encoded_addresses . '"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Prefer: outlook.body-content-type="text"', 'Accept: application/json','Authorization: Bearer '.$account['access_token']));
$response = curl_exec($ch);
print_r($response);
One email address (above example) works. But if I pass in more than one email address to the participants parameter, it fails and I get ALL messages in the mailbox retrieved regardless of participants.
Code example (not working):
$encoded_addresses = urlencode("participants:email#domain.com or anotheremail#anotherdomain.com");
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $encoded_addresses . '"';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Prefer: outlook.body-content-type="text"', 'Accept: application/json','Authorization: Bearer '.$account['access_token']));
$response = curl_exec($ch);
print_r($response);
I’m using PHP and cURL.
Has anyone managed to search on multiple values? If so with what syntax?
Note I’m using search and not filter parameter.
Thanks
As you mentioned in the comment your request url is this:
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon#bbc.co.uk" or "participants:bob#bbc.co.uk"
Which is wrong. The correct forms should be:
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon#bbc.co.uk or participants:bob#bbc.co.uk"
https://graph.microsoft.com/v1.0/me/messages?$search="participants:simon#bbc.co.uk or bob#bbc.co.uk"
Try to use curl_escape instead of urlencode
$ch = curl_init();
$query = curl_escape($curl ,'participants:email#domain.com or anotheremail#anotherdomain.com');
$url = 'https://graph.microsoft.com/v1.0/me/messages?$search="' . $query . '"';
For the OR condition, you could use the below way :
"participants: email#domain.com or participants:anotheremail#anotherdomain.com"
Modification of your code will be as below :
$encoded_addresses = urlencode("participants: email#domain.com or participants:anotheremail#anotherdomain.com");

file_get_content return null for some url

why some url with json content return null on php function get page content?
i used these ways :
file_get_content
curl
http_get
but return null . but when page open with browser json content show on browser ?? anyone can help me?
$url = 'https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=9IaRpdmQzFppJMabLHbHyzBaQnm8bM';
$result = file_get_content($url);
return null but in browser show
{"error_description": "Access token has expired.", "error": "invalid_credentials"}
It returns the following:
failed to open stream: HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
So, you have to be authorized.
You ca play with this code snippet (I can't test since token expired again):
<?php
$access_token = "s9spZax2Xrj5g5bFZMJZShbMrEVjIo"; // use your actual token
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/";
// Solution with file_get_contents
// $content = file_get_contents($url . "?access_token=" . $access_token);
// echo $content;
// Solution with CURL
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Basic " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url); // . $params - if you need some params
curl_setopt($ch, CURLOPT_POST, false);
// curl_setopt($ch, CURLOPT_USERPWD, $userPwd);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($ch_error) {
throw new Exception("cURL Error: " . $ch_error);
}
if ($info["http_code"] !== 200) {
throw new Exception("HTTP/1.1 " . $info["http_code"]);
}
echo $result;

How to use transltr api

How can I call transltr api from my localhost using php? I have zero knowledge in calling api and what to type in the php file. Thank you in advance !
php cUrl library must be loaded and enabled.
You can check this using phpinfo file.
To use API, you can try code below.
<?php
$params = array('text'=>'something', 'from'=>'en', 'to'=>'de');
$data_string = json_encode($params);
$ch = curl_init('http://www.transltr.org/api/translate');
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);
if(!$result){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
echo $result;
?>
Hope this helps
You can use curl_exec function from CURL extension.
In the most simple form it looks like:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$result = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Now $result holds what you need.

how to integrate atom payment gateway in PHP

I am using ATOM payment gateway and YII framework. I am using following code & i am not getting response here $returnData = curl_exec($ch); its returning empty.
Please tell how can i come over it. is ther any tutorials for this integration.
$url = ‘http://203.114.240.77/paynetz/epi/fts';// test bed URL
$port = 80;
$atom_prod_id = “NSE”;
// code to generate token
$param = "&login=".$userid."&pass=".$password."&ttype=NBFundTransfer&prodid=".$atom_prod_id."&amt=".$amount."&txncurr=INR&txnscamt=0&clientcode=".$clientcode."&txnid=".$invoiceid."&date=".$today."&custacc=12345";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_PORT , $port);
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
$returnData = curl_exec($ch);
// Check if any error occured
if(curl_errno($ch))
{
echo ‘Curl error: ‘ . curl_error($ch);
}
curl_close($ch);
$xmlObj = new SimpleXMLElement($returnData);
$final_url = $xmlObj->MERCHANT->RESPONSE->url;
// eof code to generate token
// code to generate form action
$param = “”;
$param .= “&ttype=NBFundTransfer”;
$param .= “&tempTxnId=”.$xmlObj->MERCHANT->RESPONSE->param[1];
$param .= “&token=”.$xmlObj->MERCHANT->RESPONSE->param[2];
$param .= “&txnStage=1″;
$url = $url.”?”.$param;
// eof code to generate form action
Please check for the http response code. Are you expecting any data to be returned? You're posting data, maybe the response-body should be empty?
Please add the code below to check for the http status code.
$info = curl_getinfo($ch);
echo $info["http_code"];

Categories