I try to read out a smart contract with web3.php, which works fine now, but I always only can read out a function, that returns a single value. When I call a function that returns for example a uint8 array, then I cannot call the elements of the array with ..[index].
Web3.php: (https://github.com/sc0Vu/web3.php)
That is my callback function:
$contract->at($contractAddress)->call($functionName, function ($err, $result) use ($contract) {
if ($err !== null) {
echo "error";
throw $err;
}
if ($result) {
$supply = $result;
echo $supply;
}
});
Has anyone an idea how I can receive an array in a callback in php?
You can see the answer in the author github "https://github.com/sc0Vu/web3.php".
$newAccount = '';
$web3->personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
if ($err !== null) {
echo 'Error: ' . $err->getMessage();
return;
}
$newAccount = $account;
echo 'New account: ' . $account . PHP_EOL;
});
Related
I have a function and I want to check the status of it and echo the return.:
if (insert($order, $items, $depot) !== false) {
echo json_encode(array('response'=>'success','message'=>'Order #'.$insert().' successfully placed.'));
}else{
echo json_encode(array('response'=>'danger','message'=>'Order failed'));
}
If the function passes it returns the order ID, if it fails it returns a fail. I am having trouble with the second line, how do I echo the value it is returning?
if (($orderId = insert($order, $items, $depot)) !== false) {
echo json_encode(array('response'=>'success','message'=>'Order #'.$orderId.' successfully placed.'));
}else{
echo json_encode(array('response'=>'danger','message'=>'Order failed'));
}
OR
$orderId = insert($order, $items, $depot);
if ($orderId !== false) {
echo json_encode(array('response'=>'success','message'=>'Order #'.$orderId.' successfully placed.'));
}else{
echo json_encode(array('response'=>'danger','message'=>'Order failed'));
}
The following code is for a game. It creates an array of N consecutive stones, an the players Alice and Bob will alternatively pick two consecutive stones (subtracts the value and leaves a 'gap') each time until there is no consecutive stones. That means the array has no values without a gap in between.
I think the code for the game is complete. But when I call it only shows a boolean(true). Instead of the name of the winner.
Here is my code:
<?php
class GameOfStones {
// Define while the index contains a stone (O) or not.
const STONE = 'O';
const STONE_PAIR = 'OO';
const GAP = '';
// Save the random result in a open/public variable.
public $line;
public $winner;
// Set the winning rules for the game.
public function win($winner) {
if (is_finished === true) {
if ($nStones % 2 == 1) {
echo "Alice" . '<br>';
}
else {
echo "Bob" . '<br>';
}
}
}
// Create the indexed line of stones
public function create_line($nStones){
return array_fill(0, $nStones, 'O');
}
// Removes a pair of stones from the line at nth location.
public function remove($n)
{
while($game->is_finished() !== true) {
if(substr($this->line, $n-1, 2) == self::STONE)
$this->line =
substr_replace($this->line, self::GAP , $n-1, 2);
else
throw new Exception('Invalid move.');
}
}
// Check if there are no further possible moves.
public function is_finished()
{
return strpos($this->line, self::STONE_PAIR) === false;
}
};
$game = new GameOfStones(rand(1,10000000));
var_dump($game->is_finished());
$game->remove(rand($n));
var_dump($game->is_finished());
var_dump($game->win());
echo $nStones;
printf($winner);
?>
It looks like you're calling a constant is_finished instead of your method is_finished(). Try replacing
public function win($winner) {
if (is_finished === true) {
if ($nStones % 2 == 1) {
echo "Alice" . '<br>';
}
else {
echo "Bob" . '<br>';
}
}
}
With
public function win($winner) {
if ($this->is_finished() === true) {
if ($nStones % 2 == 1) {
echo "Alice" . '<br>';
}
else {
echo "Bob" . '<br>';
}
}
}
boolean(true) is the return value you are getting from your first var_dump in var_dump($game->is_finished());
In remove() function, $game variable doesn't exist. Instead you should check for $this since remove() is already a class function
while($this->is_finished() !== true)
Also, in win() function, you are not calling is_finished function correctly.
if ($this->is_finished() === true)
Why strpos PHP not work with fsockopen response ?
When load this code. This code will be requests sdgsgsdgsfsdfsd.ca to whois.cira.ca server and find text Domain status: available with strpos PHP if found it's will be echo
{"domain":"sdgsgsdgsfsdfsdca","availability":"available"}
but if not found text. It's will be echo
{"domain":"sdgsgsdgsfsdfsdca","availability":"TAKEN"}
In this case found text but still echo
{"domain":"sdgsgsdgsfsdfsdca","availability":"TAKEN"}
How can i do ?
<?php
$server = "whois.cira.ca";
$response = "Domain status: available";
showDomainResult(sdgsgsdgsfsdfsd.ca,$server,$response);
function checkDomain($domain_check,$server,$findText)
{
$con = fsockopen($server, 43);
if (!$con) return false;
fputs($con, $domain_check."\r\n");
$response = ' :';
while(!feof($con))
{
$response .= fgets($con,128);
}
echo $response."<BR><BR><BR><BR><BR>";
fclose($con);
if (strpos($response, $findText))
{
return true;
}
else
{
return false;
}
}
function showDomainResult($domain_check,$server,$findText)
{
if (checkDomain($domain_check,$server,$findText))
{
class Emp
{
public $domain = "";
public $availability = "";
}
$e = new Emp();
$e->domain = $domain_check;
$e->availability = "available";
echo json_encode($e);
}
else
{
class Emp
{
public $domain = "";
public $availability = "";
}
$e = new Emp();
$e->domain = $domain_check;
$e->availability = "TAKEN";
echo json_encode($e);
}
}
?>
you're using strpos wrong, if the string START with what you're searching for, it will return int(0), which is "kinda false" by PHP's definition. explicitly check for false, like this
return false!==strpos($response, $findText);
and make sure you're using !== not !=
and as a rule of thumb, never use loose comparison operators in PHP if you can avoid it, hilarious bugs can occur if you do: https://3v4l.org/tT4l8
For quite a while now we experience a very weird problem with our hosting server. Once a while (seems randomly) variables in PHP become NULLs.
In general everything works perfectly fine, but once a while it happens. All accounts on the server are affected and all PHP apps (including PHPMyAdmin, Wordpress our own scripts). We contacted our hosting company, but they are unable to find any solution.
I had few ideas, the most promising one was an issue with Suhosin. But I do not get any message in the log directly from it.
We made a simplest possible script to reproduce the error:
<?php
class Example
{
protected $stringVar = 'this is a string value';
public function accessParameter()
{
$error = false;
if (isset($this->stringVar) && !is_null($this->stringVar)) {
echo "string var : " . $this->toStringWithType($this->stringVar) . "\n";
} else {
echo "string var is not set\n";
$error = true;
}
if ($error) {
$logfile = dirname(__FILE__)."/random_bug_log.log";
file_put_contents($logfile, date('Y-m-d H:i:s')."\n", FILE_APPEND);
file_put_contents($logfile, $this->toStringWithType($this->stringVar) . "\n", FILE_APPEND);
}
}
public function toStringWithType($var)
{
$type = gettype($var);
return "($type) '$var'";
}
}
$e = new Example();
$e->accessParameter();
Normal output:
string var : (string) 'this is a string value'
Output when the weird thing happens:
string var is not set
I open to any ideas or suggestions how to solve this problem. I guess the ultimate solution is to change the hosting company. I did not manage to create this issue on localhost or any other server.
Test piece that have been made, including your suggestions:
<?php
class Example
{
protected $stringVar = 'this is a string value';
public function accessParameter() {
$error = false;
if(isset($this->stringVar) && !is_null($this->stringVar)) {
echo "string var : "
.$this->toStringWithType($this->stringVar)
."\n";
} else {
echo "string var is not set\n";
$error = true;
}
if($error) {
$logfile = dirname(__FILE__)."/random_bug_log.log";
file_put_contents($logfile, date('Y-m-d H:i:s')." ", FILE_APPEND);
file_put_contents($logfile,
$this->toStringWithType($this->stringVar) . "\n",
FILE_APPEND);
}
}
public function writeParameter() {
$this->stringVar="variable assigned";
if(isset($this->stringVar) && !is_null($this->stringVar)) {
echo "string var : "
.$this->toStringWithType($this->stringVar)
."\n";
} else {
echo "string var is not set\n";
$error = true;
}
}
public function toStringWithType($var)
{
$type = gettype($var);
return "($type) '$var'";
}
}
$e = new Example();
$e->accessParameter();
$e->writeParameter();
The output while the thing happens:
string var is not set
string var is not set
it is very strange problem.
it may not be a solution but worth to try;
protected $stringVar;
function __construct() {
$this->stringVar = 'this is a string value';
}
I would recommend to use !== instead of is_null to see if the variable is actually null.
if (isset($this->stringVar) && ($this->stringVar !== null)) {
or
if (isset($this->stringVar) && (!empty($this->stringVar)) {
should do the work too.
In case of theses type of issues check with the value that you have in if condition and do what you want in else. Like in your situation do like:
if(isset($this->stringVar) && ($this->stringVar == "this is a string value")) {
}else{
// your code here...
}
The value is AbcDefg_123.
Here is the regex:
function checkAlphNum($alphanumeric) {
$return = false;
if((preg_match('/^[\w. \/:_-]+$/', $alphanumeric))) {
$return = true;
}
return $return;
}
Should allow a-zA-Z0-9.:_-/ and space in any order or format and does need all but at least one character.
EDIT: Sorry again, looks like var_dump() is my new best friend. I'm working with XML and it's passing the tag as well as the value.
#SilentGhost thnx for the tips.
It works for me too.
<?php
class RegexValidator
{
public function IsAlphaNumeric($alphanumeric)
{
return preg_match('/^[\w. \/:_-]+$/', $alphanumeric);
}
}
?>
and this is how I am testing it.
<?php
require_once('Classes/Utility.php');
$regexVal = new RegexValidator();
$list = array("abcd", "009aaa", "%%%%", "0000(", "aaaa7775aaa", "$$$$0099aaa", "kkdkdk", "aaaaa", "..0000", " ");
foreach($list as $value)
{
if($regexVal->IsAlphaNumeric($value))
{
echo $value . " ------>passed";
echo "<BR>";
}
else
{
echo $value . "------>failed";
echo "<br>";
}
}
?>
function checkAlphNum($alphanumeric) {
$return = false;
if((preg_match('/^[A-Za-z0-9\w. \/:_-]+$/', $alphanumeric))) {
$return = true;
}
return $return;
}
print checkAlphNum("AbcDefg_123");
Returns true.