I use the following code to send a get request to boilerpipe java web api to extract the html contents into plain text of a website,i use telerivet webhook api to send and recieve messages to my server where the php file is located,the timeout provided is 10 sec,i get timeout always with this code,please help me out
if ($_POST['secret'] !== $webhook_secret)
{
header('HTTP/1.1 403 Forbidden');
echo "Invalid webhook secret";
}
else
{
if ($_POST['event'] == 'incoming_message')
{
$content = $_POST['content'];
$from_number = $_POST['from_number'];
$phone_id = $_POST['phone_id'];
// do something with the message, e.g. send an autoreply
header("Content-Type: application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,
'http://boilerpipe-web.appspot.com/extract?url=http://www.kvgengg.com&extractor=DefaultExtractor&output=text&extractImages='
));
$content = curl_exec($ch);
echo $content;
}
}
It seems that there is a syntax error as you have an extra parentheses in the url, I've removed it.
As well if you use http_build_query to pass in your parameter it should solve your problem
if ($_POST['secret'] !== $webhook_secret)
{
header('HTTP/1.1 403 Forbidden');
echo "Invalid webhook secret";
}
else
{
if ($_POST['event'] == 'incoming_message')
{
$content = $_POST['content'];
$from_number = $_POST['from_number'];
$phone_id = $_POST['phone_id'];
// do something with the message, e.g. send an autoreply
header("Content-Type: application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,
'http://boilerpipe-web.appspot.com/extract?' .
http_build_query(array(
'url' => 'http://www.kvgengg.com',
'extractor' => 'DefaultExtractor'
'output' => 'text',
'extractImages' => ''
))
);
$content = curl_exec($ch);
echo $content;
}
}
Related
if (strpos($message, "/translate") === 0) {
$word = substr ($message, 10);
$mymemori = json_decode(file_get_contents("https://api.mymemory.translated.net/get?q=".$word."&langpair=en|id"), TRUE)["matches"]["translation"];
file_get_contents($apiURL."/sendmessage?chat_id=".$chatID."&text=Hasil translate: ".$word." : $mymemori ");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_string);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if(($html = curl_exec($ch)) === false) {
echo 'Curl error: ' . curl_error($ch);
die('111');
}
}
Hello guys i was trying to make translation bot on telegram using php but the output is still error or no output at all. Am using this API https://api.mymemory.translated.net/get?q=Hello%20World!&langpair=en|id
Please help how to get the translation
Error output IMAGES
First, I suggest using cURL, Not file_get_contents.
Second, No need to echo anything, Because the URL will be visited by webhook, Not a human.
Third, You need a method to send requests to Telegram Bot API.
Use this new code:
define('Token', '<your_bot_token>');
# Reading the update from Telegram
$update = json_decode(file_get_contents('php://input'));
$message = $update->message;
$text = $message->text;
if (strpos($text, '/translate') === 0) {
$word = substr ($message, 10);
$mymemori = json_decode(file_get_contents("https://api.mymemory.translated.net/get?q=".$word."&langpair=en|id"), TRUE)["matches"]["translation"];
//
Bot('sendMessage', [
'chat_id' => $update->message->chat->id,
'text' => "Hasil translate: $word : $mymemori "
]);
}
function Bot(string $method, array $params = [])
{
$ch = curl_init();
$api_url = 'https://api.telegram.org/bot' . Token . "/$method";
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
if ($result->ok == false)
{
throw new Exception($result->description, $result->error_code);
}
return $result->result;
}
I'm trying to implement reCAPTCHA in my website, everything seems working fine, except the return from file_get_contents().
Here is my code:
if ($_REQUEST["send"] == 1){
// access
$secretKey = 'my_key';
$captcha = $_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
echo ($responseKeys);exit;
if(intval($responseKeys["success"]) !== 1) {
$message = 'Invalid reCAPTCHA';
} else {
$msg = 'content';
send_mail('send_to',"Subject",$msg);
header("location:index.php?send=1");exit;
}
}
My variable response is returning empty.
I tried to open https://www.google.com/recaptcha/api/siteverify? inserting manually the variables and it seems to work fine.
Am I forgeting something?
Thanks
Their API waiting for a POST request. Your code send GET request.
See answer here How to post data in PHP using file_get_contents?
My wrappers were disabled, reason why I couldn't reach the URL and get the return.
As I don't have access to php.ini the workaround was send the request with curl, here is the code:
$url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo curl_error($ch);
echo "\n<br />";
$response = '';
} else {
curl_close($ch);
}
if (!is_string($response) || !strlen($response)) {
echo "Failed to get contents.";
$contents = '';
}
$responseKeys = json_decode($response,true);
I'm trying to call a php from another php passing data to it and getting the return value. The two php's are on different domains.
First php:
$url = 'http://myweb.com/custom-php/createCat.php';
$data = array('name' => $name);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
Second php (It's on a Wordpress site):
header("Access-Control-Allow-Origin: *");
require('../wp-load.php');
$name = $_REQUEST['name'];
echo $name;
if(isset($name))
{
echo wp_create_category($name,0);
}
else
{
echo false;
}
I get the following error:
file_get_contents(http://myweb.com/custom-php/createCat.php): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
But if i access it via http://myweb.com/custom-php/createCat.php?name=test it works ok.
The request error could be because the allow_url_fopen PHP.ini directive is set off in the remote URL.
So an alternative may be using CURL:
<?php
$url = 'http://myweb.com/custom-php/createCat.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result= curl_exec($curl);
curl_close($curl);
?>
Answer to the comment 'how can i send the data i need(a variable with some text)':
Use CURLOPT_POSTFIELDS:
curl_setopt($curl, CURLOPT_POSTFIELDS, "name=test&var=" . urlencode($someText));
When you visit the url you make a get request.
You should do the same in your php:
$url = 'http://myweb.com/custom-php/createCat.php';
$data = array('name' => $name);
$result = file_get_contents($url . '?' . http_build_query($data));
echo $result;
Also, you need to check if the $_REQUEST key is set before using it:
//this header is only required for ajax..
header("Access-Control-Allow-Origin: *");
require('../wp-load.php');
if(isset($_REQUEST['name'];))
{
echo wp_create_category($_REQUEST['name'],0);
}
else
{
echo false;
}
I want to send http header to Api and get json response.
I have all book detail in books table and I want to get all books.
But i have 5 http header to get access them.
Client-Service,Auth-Key,Content-Type,User-ID,Authorization
Url to get details:
http://127.0.0.1/RestApi/index.php/book/
Controller Code:
public function index() {
$method = $_SERVER['REQUEST_METHOD'];
if ($method != 'GET') {
json_output(400, array('status' => 400, 'message' => 'Bad request.'));
} else {
$check_auth_client = $this->MyModel->check_auth_client();
if ($check_auth_client == true) {
$response = $this->MyModel->auth();
if ($response['status'] == 200) {
$resp = $this->MyModel->book_all_data();
json_output($response['status'], $resp);
}
}
}
}
Model Code:
public function book_all_data()
{
return $this->db->select('id,title,author')->from('books')->order_by('id','desc')->get()->result();
}
I want to access to access on button click but how send http header to rest api page and get all data using codeigniter ?
Use CURL to set headers. See below example. Hope it will help you
$headers = array(
'Client-Service:CLIENT_SERVICE_DETAIL',
'Auth-Key:YOUR_AUTH_KEY',
'Content-Type:YOUR_CONTENT_TYPE',
'User-ID:YOUR_USER_ID',
'Authorization:YOUR_AUTHORIZATION',
);
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, YOUR_URL);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_HEADER, 1);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
$buffer = curl_exec($curl_handle);
$header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
$body = substr($buffer, $header_size);
curl_close($curl_handle);
When I am using GCM for push notification , I got an error return as: field "data" must be a JSON array.
When user create the new post then notification will be send to all Registered devices. Any one have some idea of how to solve it? Thank you.
function Notification($post) {
global $wpdb;
$pub_post = get_post($post_ID);
$post_title=$pub_post->post_title;
$totalrecord = $this->get_allrecord();
$message = "Your New post, " .$post_title." has been published";
if (count($totalrecord) > 0) {
//$display_row = null;
foreach ($totalrecord as $row) {
$a = $row->token;
$this->sendPushNotification($a, $message);
}
}
}
function get_allrecord(){
global $wpdb;
$results =$wpdb->get_results('SELECT token FROM wp_push_tokens ', OBJECT);
return $results;
}
function sendPushNotification($registration_ids, $message) {
$apiKey = "xxxxxxxxxxxxxxxxxxxxxxx";
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$fields = array(
'register' =>$registration_ids,
'data' =>$message );
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true );
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, wp_json_encode($fields));
// Execute post
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
// Close connection
curl_close($ch);
return $result;
}
Your content type is "application/json", which means the "data" field must be a JSON of the form :
"data": {
"message": "your message"
}
Note that the "message" key in this example is custom. You can use whatever keys you wish, and your app will have to search for those keys when it receives the message.
I don't know PHP, but something like this may work :
$fields = array(
'registration_ids' =>$registration_ids,
'data' => array('message' => $message));