Dynamic keyboard in PHP Telegram bot - php

I have this snippet for creating dynamic keyboard:
$keyboard_array = [];
foreach ($verse_arr['items'] as $item) {
$keyboard_array[] = ["text" => '"'. $item['poem']['verse-r'] . '
' . $item['article']['verse-l'] . '
' . $item['article']['poet'] . '
' . $item['article']['book'] . '"' , "callback_data" => '"'.$item['article_id'].'"'];
}
$current = print_r($keyboard_array,true);
$file = 'keyboard.log';
file_put_contents($file, $current);
apiRequestJson("sendMessage", array('chat_id' => $chat_id, "text" => "Search results: ", 'reply_markup' => new InlineKeyboardMarkup(
[
'inline_keyboard' => [$keyboard_array]
])));
As you can see in rows 8-10 I write the results to a file and everything looks fine there. But the message doesn't get through.The text is in UTF-8 and Persian by the way.
I hope you can help me with this.
Regards,

Related

Fill RTF placeholder with php string that contains line breaks and tabs

I have a function on my php site that fills rtf templates with customer data.
It replaces placeholders that look like this %%PLACEHOLDER%%.
Now I want to replace a placeholder with a string that has tabs and line breaks in it. I can't figure out how to do this. I did read this post but the anwser doesn't work for me. In the final file it just displays {\pard\par} in plain text.
Replace function:
function populate_RTF($vars, $doc_file)
{
$replacements = array ('\\' => "\\\\",
'{' => "\{",
'}' => "\}");
$document = file_get_contents($doc_file);
if(!$document) {
return false;
}
foreach($vars as $key=>$value) {
$search = "%%".strtoupper($key)."%%";
foreach($replacements as $orig => $replace) {
$value = str_replace($orig, $replace, $value);
}
$document = str_replace($search, $value, $document);
}
return $document;
}
File where I use the function:
function MakeString()
{
foreach(/*for each here*/)
{
$string = $name. "\tab" . $gpu . "\tab" . $cpu . "{\pard\par}";
$string .= $name. "\tab" . $gpu . "\tab" . $cpu . "{\pard\par}";
$string .= $name. "\tab" . $gpu . "\tab" . $cpu . "{\pard\par}";
}
return $string;
}
$vars = array
(
'name' => $info['name'],
'address' => $info['address'],
'phone' => $info['phone'],
'string-here' => MakeString(),
);
$new_rtf = populate_RTF($vars, $template);
$fr = fopen($saveLocation.$filename, 'w') ;
fwrite($fr, $new_rtf);
fclose($fr);
RTF output now is:
John \tabAsus GTX 1060\tabIntel I7{\pard\par}Mike\tabAsus GTX 1080\tabAMD FX 8120{\pard\par}
RTF output should be:
John Asus GTX 1060 Intel I7
Mike Asus GTX 1080 AMD FX 8120
Someone that can help me with this?

Why is PHP Foreach Loop only extracting last item in the element of JSON Array?

I'm extracting data from a JSON file and have no problem getting all the items except for one because it is an array within the JSON array.
Here is a sample of the JSON array:
{
"items": [
{
"id": "12345",
"snippet": {
"publishedAt": "2015-07-25T02:43:39.000Z",
"channelTitle": "BuzzFeedVideo",
"tags": [
"celebrity",
"celebrities",
"seven",
"seven years",
"years",
"different",
"now",
"then",
"ariana grande",
"justin bieber",
"calvin harris",
"miley cyrus",
"abigail breslin",
"daniel radcliffe",
"neville longbottom",
"matthew lewis",
"buzzfeed",
"buzzfeedvideo",
"buzzfeedyellow",
"video"
],
"defaultAudioLanguage": "en"
},
"statistics": {
"viewCount": "700146",
"likeCount": "16847",
"dislikeCount": "596",
"favoriteCount": "0",
"commentCount": "1563"
}
}
]
}
I can get id, snippet:publishedAt, but when it comes to tags, only the last item in the tags array "video" is extracted with my foreach loop.
Here is my foreach loop:
$tags = $videosResponse['items'][0]['snippet']['tags'];
foreach($tags as $tag):
$keywords = $tag;
echo $keywords;
endforeach;
How do I get all the items within the JSON tags array starting from "celebrity" down to "video"?
UPDATE:
What I'm trying to do is update a php array file with the JSON data using file_put_contents.
Here is the structure I'm using.
$tags = $videosResponse['items'][0]['snippet']['tags'];
foreach($tags as $tag):
$keywords = strtolower(replace_utf8_chars($tag));
$new_array_line = " '" . $id . "' => array\n\t(\n\t'datetime' => '" . $publishedAt . "',\n\t'title' => '" . addslashes($title) . "',\n\t'description' => '" . addslashes($description) . "',\n\t'channel title' => '" . addslashes($channelTitle) . "',\n\t'tags' => '" . $keywords . "',\n\t'duration' => '" . $duration . "',\n\t'viewCount' => '" . $viewCount . "',\n\t'likeCount' => '" . $likeCount . "',\n\t'commentCount' => '" . $commentCount . "',\n\t'url' => '" . $url . "'\n\t),";
endforeach;
and then the array pop and file put contents code is just below the $new_array_line variable.
if(!array_key_exists($id, $videoids)) {
$id_content = file($videoids_file); // Parse file into an array by newline
$id_data = array_pop($id_content);
if (trim($id_data) == ');?>') {
$id_content[] = "\n" . ' // ' . $_SERVER['REMOTE_ADDR'] . ' | ' . date('M j, y h:i:s A') . "\n"; // Echo debug line
$id_content[] = $new_array_line;
$id_content[] = "\n".$id_data;
file_put_contents($videoids_file, implode($id_content));
}
return array('title' => $title, 'description' => $description, 'channelTitle' => $channelTitle);
}
If I end the foreach after this last part, then the first item in the tags array is output, but if I end the foreach before the if statement, then only the last item in the tags array is output.
Do I need some sort of time delay before the if statement is executed?
$new_array_line = need to by replaced by $new_array_line .= otherwise the foreach loop will keep overriding it when evaluating the next tag value until keeping the last one which is related to the 'video' tag.
never heared of endforeach...
When the JSON is valid, you should be able to access the array with:
$videosResponse['items'][0]['tags'];

Sorting array in php (OpenCart addresses)

I am trying to sort the array $result by 'address' below. I have tried usort() but it does not work.
Thanks for all help
foreach ($results as $result) {
$this->data['addresses'][] = array(
'address_id' => $result['address_id'],
'address' => $result['firstname'] . ' ' . $result['lastname'] . ', ' . $result['address_1'] . ', ' . $result['city'] . ', ' . (($result['zone']) ? $result['zone'] . ', ' : FALSE) . (($result['postcode']) ? $r$
'href' => HTTPS_SERVER . 'index.php?route=account/address/' . $type . '&address_id=' . $result['address_id']
);
}
//The following does not work
usort($result, 'address');
To make usort work, you need to define a custom function:
usort($result, function($a, $b){
// ^ anonymous function
return strcmp($a['address'], $b['address']);
});
usort needs a function as second parameter to sort your data, 'address' doesn't seem to be a function
You try to sort $result but you fill $this->data['addresses'] with data
I would fill $this->data['addresses'] with data and use the thing you want to use as key and then use ksort to sort it, example:
foreach ($results as $result) {
$this->data['addresses'][$result['address_id']] = array(
'address_id' => $result['address_id'],
'address' => $result['firstname'] . ' ' . $result['lastname'] . ', ' . $result['address_1'] . ', ' . $result['city'] . ', ' . (($result['zone']) ? $result['zone'] . ', ' : FALSE) . (($result['postcode']) ? $r$
'href' => HTTPS_SERVER . 'index.php?route=account/address/' . $type . '&address_id=' . $result['address_id']
);
}
ksort($this->data['addresses']);

DOMDocument with Quotations in the load URL

I am displaying contents of an XML file of a search URL from Wordpress using domdocument. Everything works on a normal search url, but when I want the search to be 'exact match' of the phrase, which means I have to put double quotes around the keyphrase, it returns nothing. So how do I get it to work when adding the quotations as shown in the URL below...
$rss = new DOMDocument();
$rss->load('' . home_url() . '/?s="' . ucfirst($player_data->first_name) . '+' . ucfirst($player_data->last_name) . '"&post_type=post&feed=rss2');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
//foreach($item as $moment); {
if (!$item==NULL) {
for($x=0;$x<10;$x++) {
$woohoo = str_replace(' & ', ' & ', $feed[$x]['title']);
$goto = $feed[$x]['link'];
$timex = $feed[$x]['desc'];
$dibidy = date('l F d, Y', strtotime($feed[$x]['date']));
$str_view_player .= '<div><strong>'.$woohoo.'</strong></div>';
// $str_view_player .= '<small><em>Posted on '.$dibidy.'</em></small></p>';
// $str_view_player .= '<p>'.$timex.'</p>';
}
} else {
$str_view_player .= '' . ucfirst($player_data->first_name) . ' ' . ucfirst($player_data->last_name) . ' has not been mentioned yet - but he will soon, we are sure of it!';
}
Notice the quote marks after s= and before &post_type
Either replace your quotation marks with %22 or use urlencode():
$url = '' . home_url() . '/?s="' . ucfirst($player_data->first_name) . '+' . ucfirst($player_data->last_name) . '"&post_type=post&feed=rss2';
$url = urlencode($url);
$rss->load($url);

PHP: How to add variables and quotes to a variable

How can I add variables and quotes to a variable?
in the output it just prints the variables
This is the code I have tried
$pl2 = '{"comment":"' . $nmp3 . '","file":"' . $pmp3 . '"},';
Try with:
$pl2 = json_encode(array(
'comment' => $nmp3,
'file' => $pmp3
));
Try this, it should work:
$p = ' {"comment": ' . $nmp3;
$p = $p.' "," file " : " ' . $pmp3;
$p=$p.' "}," ';
echo $p;

Categories