execute php echo as parameter value - php

I have below code & I am generating tracking_id values manually & its working fine :
<?php
$data = [
"client_reference_id" => "ABCD",
"tracking_id" => "1234",
];
$data = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";
Result :
{"response":[{"tracking_id":"1234",}],
Now i need to create tracking_id dynamically.... so i tried like below :
"tracking_id" => "$r = 'DOCC'. mt_rand(0000000001,9999999999); echo $r;",
I got below Result :
{"response":[{"tracking_id":" = 'DOCC'. mt_rand(0000000001,9999999999); echo ;","
But i should get some random number as tracking_id....
Means php code inside parameter is not working....

As your require 10 digit random number:
function randomNumber($length) {
$result = '';
for($i = 0; $i < $length; $i++) {
$result .= mt_rand(1, 9);
}
return $result;
}
$data = [
"client_reference_id" => "ABCD",
"tracking_id" => 'DOCC'.randomNumber(10);//no need to echo, just assign it
];
$data = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$curl_response = curl_exec($curl);
curl_close($curl);
echo $curl_response ."\n";

Refactor your code:
$r = 'DOCC'. mt_rand(0000000001,9999999999);
$data = [
"client_reference_id" => "ABCD",
"tracking_id" => $r,
];

Just replace $data array with the following.
$data = [
"client_reference_id" => "ABCD",
"tracking_id" => 'DOCC'. mt_rand(0000000001,9999999999),
];

Related

Redirect / Curl on console returns different result than DOMDocument()->loadHTMLFile()

I would like to evaluate the data for the url https://poesie.bretten.wiki/abtauchen with php,
which curl delivers on the console. However, in PHP I get the values ​​of the redirect and not the original values.
Values for <meta property="og:image">
curl (Linux) curl https://poesie.bretten.wiki/abtauchen | gedit -
https://blogger.googleusercontent.com/img/a/...
loadHTMLFile() -> returns the values ​​of the redirect.
https://www.aphorismen.de/files/bild-fuer-facebook.jpg
$u='https://poesie.bretten.wiki/abtauchen';
define("TITEL", "title");
define("BESCHREIBUNG", "description");
define("BILD", "image");
define("LINK", 'url');
define("QUERY", "Query");
define("WERT", "Wert");
define("ATTR_CONTENT", 'content');
$Keys[TITEL] = TITEL;
$Keys[BESCHREIBUNG] = BESCHREIBUNG;
$Keys[BILD] = BILD;
$Keys[LINK] = LINK;
$data[$Keys[TITEL]] = [QUERY => TITEL, WERT => null];
$data[$Keys[BESCHREIBUNG]] = [QUERY => "meta[#name='description']", WERT => null];
$data[$Keys[BILD]] = [QUERY => "meta[#property='og:image']", WERT => null];
$data[$Keys[LINK]] = [QUERY => "meta[#property='og:url']", WERT => null];
/*
$ch = curl_init($u);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $u);
$u = curl_exec($ch);
curl_close($ch);
*/
$xml = new DOMDocument();
$xml->loadHTMLFile($u);
$xpath = new DOMXpath($xml);
foreach ($Keys as $key)
{
$a="/html/head/".$data[$key][QUERY];
$item=$xpath->query($a);
if ((null != $item) && ($item->count() > 0))
{
$a = null;
if ($item[0]->hasAttribute(ATTR_CONTENT)){
$a=$item[0]->getAttribute(ATTR_CONTENT);}
else
$a=$item[0]->nodeValue;
$data[$key][WERT] = $a;
}
}
var_dump($data);```
Whatsapp can handle the data:

I can't merge these arrays correctly

From the Proxmox API, I am trying to get a list of nodes on which each VM is hosted in order to create the following array:
Array:
( [vm1] => node1 [vm2] => node2 [vm3] => node2 [vm4] => node2 )
My code:
<?php
$vmArray = array();
$multipleVmArray = array();
$nodeArray = array();
$multipleNodeArray = array();
if (empty($onlinenode)) {
echo "<br>All nodes are down.<br>";
}
else {
foreach ($servers as $server) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$onlinenode.'/api2/json/nodes/'.$server['node'].'/qemu');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $curlTimeOut);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Cookie: PVEAuthCookie=' .$cookie;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Node ' .$server['node']. ' is down (error : ' . curl_error($ch) . ').<br>';
}
else {
$arrays = json_decode($result, true);
$vmcount = count((array)$arrays['data']);
if ($vmcount == 0) {
echo 'Node ' .$server['node']. ' doesn\'t host vm.<br>';
}
else if ($vmcount == 1) {
array_push($vmArray,$arrays['data']['0']['name']);
array_push($nodeArray,$server['node']);
}
else {
$vmNames = array_column($arrays['data'], 'name');
array_push($multipleVmArray,$vmNames);
$nodeArrayFill = array_fill(0, $vmcount, $server['node']);
array_push($multipleNodeArray,$nodeArrayFill);
}
}
curl_close($ch);
}
}
$mergedVmArray = array_merge($vmArray,$multipleVmArray);
$mergedNodes = array_merge($nodeArray,$multipleNodeArray);
$combinedArrays = array_combine($mergedVmArray,$mergedNodes);
print_r($combinedArrays);
?>
But result is : Array ( [winxp] => pve1 [winxp4] => pve3 [Array] => Array ( [0] => pve2 [1] => pve2 ) )
(Pay no attention to the names of the VMs and nodes)
And I would like : Array ( [vm1] => node1 [vm2] => node2 [vm3] => node2 [vm4] => node2 )
I don't understand why the below code works but not when I implement on mine :
<?php
$vmArray = array();
$multipleVmArray = array();
$nodeArray = array();
$multipleNodeArray = array();
array_push($vmArray,"vm1");
array_push($multipleVmArray,"vm2", "vm3", "vm4");
array_push($nodeArray,"node1");
array_push($multipleNodeArray,"node2", "node2", "node2");
$mergedVmArray = array_merge($vmArray,$multipleVmArray);
$mergedNodes = array_merge($nodeArray,$multipleNodeArray);
$combinedArrays = array_combine($mergedVmArray,$mergedNodes);
print_r($combinedArrays);
?>
Any idea?

telegram bot split keyboard rows[columns]

I am writing my telegram bot codes in PHP. I would like to split my inline keyboard into 2 or 3 columns. Here is my code:
foreach ($categories as $cat) {
$key[] = array(
array('text'=>$cat['name'],'callback_data'=>'sub-'.$cat['id'])
);
if ($k % 2 == 0) {
$keyoptions[] = $key;
$key = array();
}
$k++;
}
$telegram->SendMessage($userid, $keyoptions);
but my code doesn't work. Where is the problem and how can I solve my issue?
EDIT :
i just used this code
$keyoptions = array_chunk($keyoptions,3);
but still can't find the problem;
Telegram API: inline_keyboard: Array of Array of InlineKeyboardButton
Example:
keyboard: [
["uno :+1:"],["uno \ud83d\udc4d", "due"],["uno", "due","tre"]
]
I don't know what library you are using and what are those fields in your code, but this a working with native telegram API:
function inlineKeyboard($text, $chatID, $btnNames, $callBackDatas)
{
$inlineRow = array(); // this is array for each row of buttons
$i = 0;
foreach ($btnNames as $name) {
array_push($inlineRow, array("text" => $name, "callback_data" => $callBackDatas[$i]));
$i++;
}
/* if you need multiple rows then just create other inlineRow arrays
and push to this array below */
$inlineKeyboard = array($inlineRow);
$keyboard = array(
"inline_keyboard" => $inlineKeyboard
);
$postfields = array
(
'chat_id' => "$chatID",
'text' => $text,
'reply_markup' => json_encode($keyboard)
);
send('sendMessage', $postfields);
}
define('BaseURL', 'https://api.telegram.org/bot<TOKEN>');
function send($method, $datas)
{
$url = BaseURL . "/" . $method;
if (!$curld = curl_init()) {
exit;
}
curl_setopt($curld, CURLOPT_POST, true);
curl_setopt($curld, CURLOPT_POSTFIELDS, $datas);
curl_setopt($curld, CURLOPT_URL, $url);
curl_setopt($curld, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curld);
curl_close($curld);
return $output;
}

Php Curl to Json

I can get data from a website with CURL.
I want to convert this data to json.
my code:
<?php
function Curlconnect($start,$end,$website) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $website);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$website = curl_exec($ch);
preg_match_all('#'.$start.'(.*?)'.$end.'#si',$website,$ver);
return $ver[1];
curl_close($ch);
}
function nt($start,$bit,$data,$a) {
preg_match_all('#'.$start.'(.*?)'.$bit.'#si',$data,$ver);
return $ver[1];
}
$url = 'http://www.url.com';
$getdata = Curlconnect('<h4','h4>',$url);
for ($a=0; $a<count($getdata); $a++) {
$printdata = nt('>','</',$getdata[$a],$a);
echo $printdata[0]. '<br />';
}
?>
Output:
1
27
32
66
94
I want to convert this data to json like that:
{
"data":{
"numbers":
[
"1",
"27",
"32",
"66",
"94",
]
}
}
How Can I do that?
Thank you very much.
Please try this
$url = 'http://www.url.com';
$getdata = Curlconnect('<h4','h4>',$url);
$jsonData = ["data"];
$jsonData["numbers"] = [];
for ($a=0; $a<count($getdata); $a++) {
$printdata = nt('>','</',$getdata[$a],$a);
$jsonData["numbers"][] = $printdata[0];
}
echo json_encode($jsonData);

How Php To Json converted?

We can convert php code how to json format?
I may have not accurately PHP coding I am beginner to learn about it because I'm new . I'll integrated into Android application.
I also draw pictures about how the information ?
for example, I want to do something like this: http://mikepenz.com/android/unsplash/pictures
<?php
// don't forget to change 'username' to your actual tumblr name
$request = 'http://walltumbler.tumblr.com/api/read/json';
$ci = curl_init($request);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);
// Tumblr JSON doesn't come in standard form, some str replace needed
$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);
// parameter 'true' is necessary for output as PHP array
$value = json_decode($input, true);
$content = $value['posts'];
// the number of items you want to display
$item = 98988;
// Tumblr provides various photo size, this case will choose the 75x75 square one
$type = 'photo-url-1280';
?>
{
"limit": null,
"offset": 0,
"count": 2442,
"total": 2442,
"data": [
<?php
for ($i=0;$i<=$item;$i++) {
if ($content[$i]['type'] == 'photo') {
echo '
{
"id": '.$i.';
"author": "Paul Jarvis",
"image_src": "' . $content[$i][$type] . '",
"color": "#7F7873",
"date": "2015-01-21 19:20:00",
"modified_date": "2014-09-01 22:36:53",
"width": 2500,
"height": 1667,
"ratio": 1.4997000694275,
"featured": 1,
"temp_id": 1
}';
$string = rtrim($item, ', ');
}
}
?>
]}
Try using the json_encode() function
<?php
$request = 'http://walltumbler.tumblr.com/api/read/json';
$ci = curl_init($request);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
$input = curl_exec($ci);
$input = str_replace('var tumblr_api_read = ','',$input);
$input = str_replace(';','',$input);
$value = json_decode($input, true);
$content = $value['posts'];
$item = 98988;
$type = 'photo-url-1280';
$photos_array = array();
for ($i=0;$i<=$item;$i++) {
if ($content[$i]['type'] == 'photo') {
$photos_array[] = array(
'id' => $i,
'author' => 'Paul Jarvis',
// Continue with all your values...
);
}
}
$json_data = array(
'limit' => null,
'offset' => 0,
'count' => 2442,
'total' => 2442,
'data' => $photos_array
);
// Then use json_encode to get your json data...
echo json_encode( $json_data );
Hope it helps

Categories