php and webservices - php

I have a wsdl file and i am trying to call it from a php class. I am using the following code:
<?php
include ("dbconn.php");
class dataclass
{
function getCountries()
{
$connection = new dbconn();
$sql = "SELECT * FROM tblcountries";
$dataset = $connection -> connectSql($sql);
return $dataset;
}
function getTest()
{
$connection = new dbconn();
$sql = mysql_query('CALL sp_getTest');
$dataset = $connection -> connectSql($sql);
return $dataset;
}
##-------------------------------------------CUSTOMER METHODS-------------------------------------------
function registerCustomer($username,$name,$surname,$password,$email,$country,$tel)
{
$connection = new dbconn();
$sql="INSERT INTO tblcustomer (customer_username, customer_password, customer_name, customer_surname,
customer_email, customer_country, customer_tel)
VALUES('$username','$name','$surname','$password','$email','$country','$tel')";
$dataset = $connection -> connectSql($sql);
}
ini_set("soap.wsdl_cache_enabled", "0");
// start the SOAP server - point to the wsdl file
$webservice = new SoapServer("http://localhost/dataobjects/myWebservice.wsdl", array('soap_version' => SOAP_1_2));
// publish methods
$webservice->addFunction("getCountries");
$webservice->addFunction("registerCustomer");
// publish
$webservice->handle();
}
?>
It is all the time giving me a problem with ini_set("soap.wsdl_cache_enabled", "0");
The error is:
Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in C:\Program Files\xampplite\htdocs\dataobjects\dataClass.php on line 47

I believe it is because you have ini_set() call in the body of your class. Put it at the top of the file or in the constructor if you have one.
class dataClass
{
function registerCustomer()
{
// some stuff
}
ini_set(/*args*/); // it's illegal to put instructions in the body of the class
}
Now I see the whole thing. You probably want to close the class with a closing backet before line 47.

You let the parser think "0" is a string, and not a number, please remove the quotes!
Edit 1 If that wouldn't work, your error must be before that line, please post the full code for review.
Edit 2 The code you provided was running inside the class, you miss a bracket before the line with ini_set.

Move the last } in front of ini_set(...)
By the way, you said you want to call a web service, but you are creating a server which may be called by others.
To call a web service, try something like this:
try{
$client = new SoapClient($service, array('location' =>"http://example.org/myWebService"));
$parameter1 = new myWebServiceParameter();
$result = $client->myWebServiceFunction($parameter1);
} catch (Exception $e) {
// handle errors
}
myWebServiceParameter must be any class with a member variable foreach WSDL message attribute of the same name.
And myWebServiceFunction is the name of the web service method.

Related

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Kit\core::__construct() on PDO class

I am currently looking at improving my PHP knowledge and creating websites to a better extent by using classes and such but I've ran into an issue when trying to allow other classes in separate files to access the PDO connection that I've sent up. I've tried hundreds of resolutions which have been inputted onto this site but can't seem to figure it out at all and can't seem to understand where I'm going wrong.
I'm being met with the following error:
[16-Sep-2022 11:05:02 UTC] PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Kit\core::__construct(), 0 passed in /home/liamapri/public_html/kit/global.php on line 24 and exactly 1 expected in /home/liamapri/public_html/kit/app/class.core.php:11
Stack trace:
#0 /home/liamapri/public_html/kit/global.php(24): Kit\core->__construct()
#1 /home/liamapri/public_html/index.php(3): include_once('/home/liamapri/...')
#2 {main}
thrown in /home/liamapri/public_html/kit/app/class.core.php on line 11
I have a global.php file which will be added to each individually page such as /home /index etc, it references all the files that I'll need and starts the session from it, see below:
error_reporting(E_ALL ^ E_NOTICE);
define('C', $_SERVER["DOCUMENT_ROOT"].'/kit/conf/');
define('A', $_SERVER["DOCUMENT_ROOT"].'/kit/app/');
define('I', 'interfaces/');
// Management
require_once C . 'config.php';
// Interfaces
require_once A . I . 'interface.engine.php';
require_once A . I . 'interface.core.php';
require_once A . I . 'interface.template.php';
// Classes
require_once A . 'class.engine.php';
require_once A . 'class.core.php';
require_once A . 'class.template.php';
// OBJ
$engine = new Kit\engine();
$core = new Kit\core();
$template = new Kit\template();
// Start
session_start();
$template->Initiate();
This references the class.engine.php first which is where I have created the database connection class, as shown below:
namespace Kit;
use PDO;
if(!defined('IN_INDEX')) { die('Sorry, this file cannot be viewed.'); }
class engine
{
public $pdo;
public function __construct()
{
global $_CONFIG;
$dsn = "mysql:host=".$_CONFIG['mysql']['hostname'].";dbname=".$_CONFIG['mysql']['database'].";charset=".$_CONFIG['mysql']['charset'].";port=".$_CONFIG['mysql']['port'].";";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$this->pdo = new PDO($dsn, $_CONFIG['mysql']['username'], $_CONFIG['mysql']['password'], $options);
} catch (\PDOException $e) {
print "<b>An error has occurred whilst connecting to the database:</b><br />" . $e->getMessage() . "";
die();
}
}
}
$connection = new engine();
I'm then trying to run a PDO query on another class (class.core.php) and keep getting met with either a PDO can't be found error or the one described above.
See the file below:
namespace Kit;
if(!defined('IN_INDEX')) { die('Sorry, this file cannot be viewed.'); }
class core implements iCore
{
public $connection;
public function __construct($connection)
{
$this->connection = $connection;
}
public function website_settings()
{
$qry = $this->connection->pdo->prepare("SELECT `website_name` FROM `kit_system_settings`");
$qry->execute([]);
if ($qry->rowCount() == 1) { while($row = $qry->fetch()) { return $row; } }
}
}
Sorry if this is a simple fix but I really can't see where I'm going wrong.
Look in your global .php on Line 24 (its probably this line: $core = new Kit\core();)
In your class.core.php you tell the constructor that it will get a connection, but in your global.php you dont give constructor (when you initialize a class) any argument.
That's what this error is telling you
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function Kit\core::__construct(), 0 passed
Just guessing, but i think
$core = new Kit\core($engine->pdo);
will fix your issue.

How to make php class available in function

I am using a third party class that works well as long as I use it in the main body of my PHP script. If I try to use it in a function that is called from main, it gets a "PHP Fatal error: Class 'RouterOS\Util' not found error". What do I need to do in the function so it can use the class?
<?php
require_once '/usr/local/sbrc/MTAPI/vendor/autoload.php';
...
GetNextRouter($loginData[0]['User'], $loginData[0]['Password'], $firstAddress[0]['IPAddress']);
...
}
function GetNextRouter($UserID, $Pass, $Address) {
$util = new RouterOS\Util($client = new RouterOS\Client($Address, $UserID, $Pass));
...
}
The error occurs on the $util = new RouterOS\Util line.
Adding
use PEAR2\Net\RouterOS;
solved my issue

PDO query in Slim causes PHP Fatal error: Uncaught RuntimeException: Unexpected data in output buffer

So my basic slim app goes along these lines:
<?php
session_start();
if (!file_exists( __DIR__ . '/settings.php')) { die("Error 500: application configuration problem."); }
require __DIR__ . '/../vendor/autoload.php';
$config = require __DIR__ . '/settings.php';
$app = new \Slim\App($config);
$container = $app->getContainer();
$container['s4s'] = function ($container) {
$db = $container['settings']['s4s'];
return new PDO('sqlsrv:Server='.$db['host'].';Database='.$db['database'], $db['username'], $db['password']);
};
$app->get('/ff', function ($request, $response) {
$sql = 'SELECT "dbo"."ZAKAZKA"."ZAKAZKA ID" FROM "dbo"."ZAKAZKA" WHERE ("ZAKAZKA ID" LIKE \'1216%\' ) ORDER BY "dbo"."ZAKAZKA"."ZAKAZKA ID" DESC';
$stmt = $this->s4s->prepare($sql);
if($stmt->execute()){
echo $stmt->debugDumpParams();
$results=$stmt->fetchAll();
}
});
this works fine and dandy. If I move the query code to a controller
<?php
namespace Glued\Playground;
use \PDO;
use Glued\Controllers\Controller;
class pdo_test extends Controller
{
public function test($request, $response)
{
$sql = 'SELECT "dbo"."ZAKAZKA"."ZAKAZKA ID" FROM "dbo"."ZAKAZKA" WHERE ("ZAKAZKA ID" LIKE \'1216%\' ) ORDER BY "dbo"."ZAKAZKA"."ZAKAZKA ID" DESC';
echo $sql;
$stmt = $this->container->s4s->prepare($sql); // this line breaks things
if($stmt->execute()){
echo $stmt->debugDumpParams();
$results=$stmt->fetchAll();
}
}
}
and add to the main code
$app->get('/playground/fff', '\Glued\Playground\Pdo_test::mytest');
it starts to break at the $stmt assignment with the error
PHP Fatal error: Uncaught RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening finalize(Object(Slim\Http\Response))\n#1 /opt/Web/html/glued/vendor/slim/slim/Slim/App.php(298): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))\n#2 /opt/Web/html/glued/public/index.php(4): Slim\App->run()\n#3 {main}\n thrown in /opt/Web/html/glued/vendor/slim/slim/Slim/App.php on line 552
since this doesnt apply to other controllers I wrote sofar, I'm either overlooking something obvious or PDO (which I dont usually use) causes the premature output. Not sure how to debug this as well.
Hints and solutions very welcome, thanks in advance!
note: I usually dont work with mssql as well, but I got the same problem against a mysql db.

How to call the assertTrue function using simpletest?

I'm new to php and I'm writing a test function to test a class I have already written. However, I'm not sure how I should properly make a call to the function assertTrue().
Here is the code I have:
<?php
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('../db/fileToBeTested.php');
class TestDbManager extends UnitTestCase {
function TestDbManager(){
$this->UnitTestCase("Test DB Manager");
}
// Function to test if isTableExisting() method works correctly
function testIsTableExisting() {
$testDB = new DB("localhost", "root", "password", "GraphAppDB", "3306", "empty#empty.com", true, "GraphApp")
$this->assertTrue($testDB->isTableExisting("users"), "users table exists");
$this->assertFalse($testDB->isTableExisting("notAValidTable"), "notAValidTable does not exist");
$this->assertFalse($testDB->isTableExisting(""));
}
}
?>
And here is the error I am getting:
Parse error: syntax error, unexpected '$this' (T_VARIABLE) in
/Applications/XAMPP/xamppfiles/htdocs/GraphApp/tests/TestDbManager.php
on line 14
You're missing a ; after
$testDB = new DB("localhost", "root", "password", "GraphAppDB", "3306", "empty#empty.com", true, "GraphApp")
The call to assertTrue is probably fine.

Am I calling this class wrong?

I have this code in a file named server.class.php:
class server
{
function addPlayer($player)
{
//Stuff
}
}
Then, I have this in a file called send.php. These are lines 38-40:
require('/var/www/server/apply/server.class.php');
$server = new server;
$server->addPlayer($_POST['IGN']);
However, I get this error when I visit my page (Php.ini is set to show all errors):
Fatal error: Class 'server' not found in /var/www/server/apply/send.php on line 39
Line 39 is $server = new server;
What am I doing wrong? I have verified that the class file is in /var/www/server/apply/server.class.php.
$server = new server()
Missed the brackets

Categories