php xml function requirements - php

hi i am using the xml function simplexml_load_string for reading the xml string but there is no any output of this function i also use dom function but the same response of this.
is there any another method of reading the xml?
or is there any modification require on server to enable these function

There are are many reasons why you might end up with no output at all. Some I can think of are:
There's a parse error in your script and your php version is not configured to show startup errors. see display_startup_errors and/or add some unconditional output to the script (so that if this output is missing you know the script didn't even reach that statement).
The script doesn't reach the statement because of some conditions ( `if (false) { ... } ). Again add some output and/or use a debugger to see if the statement is reached.
The string contains something that is not valid xml and therefore the libxml parser gives up and simplexml_load_string() returns false. Test the return value and maybe check the errors libxml may have encountered, see http://docs.php.net/function.libxml-use-internal-errors
The SimpleXML module isn't present (though in recent versions of php it's enabled by default). Use extension_loaded() and/or function_exists() to test this.
Try it again with a bit more error handling, e.g.
<?php
// this is only for testing purposes
// set those values in the php.ini of your development server if you like
// but use a slightly more sophisticated error handling/reporting mechanism in production code.
error_reporting(E_ALL); ini_set('display_errors', 1);
echo 'php version: ', phpversion(), "\n";
echo 'simplexml_load_string() : ', function_exists('simplexml_load_string') ? 'exists':"doesn't exist", "\n";
$xml = '<a>
>lalala
</b>
</a>';
libxml_use_internal_errors(true);
$doc = simplexml_load_string($xml);
echo 'errors: ';
foreach( libxml_get_errors() as $err ) {
var_dump($err);
}
if ( !is_object($doc) ) {
var_dump($doc);
}
echo 'done.';
should print something like
php version: 5.3.2
simplexml_load_string() : exists
errors: object(LibXMLError)#1 (6) {
["level"]=>
int(3)
["code"]=>
int(76)
["column"]=>
int(7)
["message"]=>
string(48) "Opening and ending tag mismatch: a line 1 and b
"
["file"]=>
string(0) ""
["line"]=>
int(3)
}
object(LibXMLError)#2 (6) {
["level"]=>
int(3)
["code"]=>
int(5)
["column"]=>
int(1)
["message"]=>
string(41) "Extra content at the end of the document
"
["file"]=>
string(0) ""
["line"]=>
int(4)
}
bool(false)
done.

Related

mongoDB php doesn't find database but is successfully connected to the server

when I connect to my mongoDB database with PHP, the variable sometimes contains an empty array but when pinging it, the array contains data. The empty arraay appears when I just restarted the apache2 webserver. I guess there are some other ways to cause it but I can't find a way on how to do that.
This is not my only issue. When I select a database, the variable contains NULL so I can't select a collection. It's also very hard to find an up to date documantation because these two are outdated.
Now to all my code:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
var_dump($mongo);
sometimes returns:
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(47) "mongodb://root:pwd#localhost:27017" ["cluster"]=> array(0) { } }
Sending a ping to the database before printing the database:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
var_dump($mongo);
returns:
object(MongoDB\Driver\Manager)#1 (2) { ["uri"]=> string(47) "mongodb://root:pwd#localhost:27017" ["cluster"]=> array(1) { [0]=> array(10) { ["host"]=> string(9) "localhost" ["port"]=> int(27017) ["type"]=> int(1) ["is_primary"]=> bool(false) ["is_secondary"]=> bool(false) ["is_arbiter"]=> bool(false) ["is_hidden"]=> bool(false) ["is_passive"]=> bool(false) ["last_is_master"]=> array(10) { ["ismaster"]=> bool(true) ["maxBsonObjectSize"]=> int(16777216) ["maxMessageSizeBytes"]=> int(48000000) ["maxWriteBatchSize"]=> int(100000) ["localTime"]=> object(MongoDB\BSON\UTCDateTime)#2 (1) { ["milliseconds"]=> string(13) "1618843900377" } ["logicalSessionTimeoutMinutes"]=> int(30) ["minWireVersion"]=> int(0) ["maxWireVersion"]=> int(6) ["readOnly"]=> bool(false) ["ok"]=> float(1) } ["round_trip_time"]=> int(0) } } }
Now my not found database:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
//Ping the database
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
//select the hltv database
$db = $mongo->hltv;
var_dump($db);
returns:
NULL
The error log shows:
PHP Warning: Undefined property: MongoDB\\Driver\\Manager::$hltv in /var/www/hltv/index.php on line 6
When trying to list all Databases:
$mongo = new MongoDB\Driver\Manager("mongodb://root:pwd#localhost:27017");
//Ping the database
$command = new MongoDB\Driver\Command(['ping' => 1]);
$mongo->executeCommand('db', $command);
//list databases
var_dump($mongo->listDatabases());
returns a blank screen and shows following error:
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\\Driver\\Manager::listDatabases() in /var/www/hltv/index.php:6\nStack trace:\n#0 {main}\n thrown in /var/www/hltv/index.php on line 6
At this point it's pointless to selecta collection.
I made sure that the user root has ReadWrite access and accessing the database with MongoDb Compass is working fine and shows all datbases/collections and lets me read/write data.
My Setup:
OS: Ubuntu 18.04
MongoDB Version: 3.6.3
PHP version: 8.0.3
MongoDB PHP Driver Version: 1.9.1
Apache2 is used to run the PHP code.
Now I found out that there is a Driver and a Library so after installing the Library everything seems to work now. I didn't know that the application was split into 2 parts.

Loop through array from NIST API.. Cant get it working

Im working with some data from the NIST CVE Database.
Im putting it through the simpleXMLElement array but cant figure out how to loop through it properly.
Tried following other threads here but cant it working.. These miltidimensional? arrays break me.
Any help would be super great!
Here is the array dump:
object(SimpleXMLElement)#1 (2) {
["channel"]=>
object(SimpleXMLElement)#2 (4) {
["title"]=>
string(31) "National Vulnerability Database"
["link"]=>
string(41) "https://web.nvd.nist.gov/view/vuln/search"
["description"]=>
string(114) "This feed contains the most recent CVE cyber vulnerabilities published within the National Vulnerability Database."
["items"]=>
object(SimpleXMLElement)#151 (0) {
}
}
["item"]=>
array(148) {
[0]=>
object(SimpleXMLElement)#3 (3) {
["title"]=>
string(35) "CVE-2008-6594 (rdf_newsfeed_export)"
["link"]=>
string(62) "https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2008-6594"
["description"]=>
string(150) "SQL injection vulnerability in the cm_rdfexport extension for TYPO3 allows remote attackers to execute arbitrary SQL commands via unspecified vectors."
}
[1]=>
object(SimpleXMLElement)#4 (3) {
["title"]=>
string(26) "CVE-2014-5129 (projectdox)"
["link"]=>
string(62) "https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2014-5129"
["description"]=>
string(162) "Cross-site scripting (XSS) vulnerability in Avolve Software ProjectDox 8.1 allows remote attackers to inject arbitrary web script or HTML via unspecified vectors."
}
[2]=>
object(SimpleXMLElement)#5 (3) {
["title"]=>
string(24) "CVE-2019-9565 (antidote)"
["link"]=>
string(62) "https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2019-9565"
["description"]=>
string(467) "Druide Antidote RX, HD, 8 before 8.05.2287, 9 before 9.5.3937 and 10 before 10.1.2147 allows remote attackers to steal NTLM hashes or perform SMB relay attacks upon a direct launch of the product, or upon an indirect launch via an integration such as Chrome, Firefox, Word, Outlook, etc. This occurs because the product attempts to access a share with the PLUG-INS subdomain name; an attacker may be able to use Active Directory Domain Services to register that name."
}
Im using this code but it just echos "item" each time..
$xml = new SimpleXMLElement($apiData);
var_dump($xml);
$i="0";
foreach ($xml->item as $key => $value) {
echo $xml['item']['$i']['link']; // get the CVE link..
echo "KEY:$key Value:$value \r\n";
$i ++;
}
You should use the -> notation for getting the link. The array index is what you called the key. So I would name that $i:
foreach ($xml->item as $i => $item) {
$link = $item->link;
echo "Index:$i Link:$link \r\n";
}

XML is not parsed in php

In my controller, I read a data from DB. (where AlarmDeatils is a stored as XML content. Eg:AlarmDeatils column contains
<SiteAlarmDetails>
<AlertId>89637</AlertId>
<SiteCode>20157498</SiteCode>
<SiteName>newport</SiteName>
<TankNumber>4</TankNumber>
<DispenserNumbedr>3</DispenserNumbedr>
<HoseNumber>3</HoseNumber>
<GradeId>11</GradeId>
<GradeName>PULP98</GradeName>
<AlarmUTCDateTime>2015-10-08T12:00:00</AlarmUTCDateTime>
<AlarmClearedUTCDateTime>2015-10-08T22:00:00</AlarmClearedUTCDateTime>
<UTCTimeZoneName>GMT Standard Time</UTCTimeZoneName>
<AlarmVolume>0</AlarmVolume>
<AlarmLevel>0</AlarmLevel>
<TankCapacity>0</TankCapacity>
<TankCapacityPercent>0</TankCapacityPercent>
<TankOverfill>0</TankOverfill>
<TankUllage>0</TankUllage>
<ProductLoss>0</ProductLoss>
<HoursElapsed>10</HoursElapsed>
<WaterLevel>0</WaterLevel>
<AvgSalesPerDay>0</AvgSalesPerDay>
<DaysToStockOut>0</DaysToStockOut>
<InvalidDataCount>0</InvalidDataCount>
<ValidDataCount>0</ValidDataCount>
<ZeroVolumeCount>0</ZeroVolumeCount>
<ZeroProductLevelCount>0</ZeroProductLevelCount>
<ZeroTotaliserAmountCount>0</ZeroTotaliserAmountCount>
</SiteAlarmDetails>
I read that that row in my controller like;
$tableAlarm = \DB::table('Alarm')
->where('Alarm.AlarmId', '=', $id)->first();
and when I use
var_dump($tableAlarm);
I get
object(stdClass)#241 (10) { ["AlarmId"]=> string(6) "245039" ["MessageNotificationId"]=> string(6) "219078" ["CompanyId"]=> string(2) "19" ["CompanyCode"]=> string(7) "MCCOLLS" ["AlertTypeId"]=> string(2) "23" ["AlarmDetails"]=> string(1408) "979381320106510Eyemouth Service Station2017-07-23T21:26:499999-12-31T23:59:59.9999999GMT Standard Time00000005000000000001-01-01T00:00:000001-01-01T00:00:000001-01-01T00:00:000000001-01-01T00:00:00" ["AlertProcessStateId"]=> string(1) "2" ["UTCDateTimeInserted"]=> string(27) "2017-07-24 02:15:36.9300000" ["UTCDateTimeUpdated"]=> string(27) "2017-07-24 02:15:36.9300000" ["RowDataVersion"]=> string(16) "00000000117D854B" }
Im trying to parse the AlarmDeatils column like;
$alertXml = simplexml_load_string( $tableAlarm->AlarmDetails);
echo $alertXml;//Nothing printed
But i'm not getting anything :( I'm trying to process that xml like;
foreach($alertXml->children() as $alerts)
{
print_r( $alerts->AlertId);// **getting SimpleXMLElement Object ()**
echo $alerts->AlertId;//**Nothing printed**
}
Hope this will be helpful. Try this simplest one.
Try this code snippet here
<?php
$xmlString=<<<XML
<SiteAlarmDetails>
<AlertId>89637</AlertId>
<SiteCode>20157498</SiteCode>
<SiteName>newport</SiteName>
<TankNumber>4</TankNumber>
<DispenserNumbedr>3</DispenserNumbedr>
<HoseNumber>3</HoseNumber>
<GradeId>11</GradeId>
<GradeName>PULP98</GradeName>
<AlarmUTCDateTime>2015-10-08T12:00:00</AlarmUTCDateTime>
<AlarmClearedUTCDateTime>2015-10-08T22:00:00</AlarmClearedUTCDateTime>
<UTCTimeZoneName>GMT Standard Time</UTCTimeZoneName>
<AlarmVolume>0</AlarmVolume>
<AlarmLevel>0</AlarmLevel>
<TankCapacity>0</TankCapacity>
<TankCapacityPercent>0</TankCapacityPercent>
<TankOverfill>0</TankOverfill>
<TankUllage>0</TankUllage>
<ProductLoss>0</ProductLoss>
<HoursElapsed>10</HoursElapsed>
<WaterLevel>0</WaterLevel>
<AvgSalesPerDay>0</AvgSalesPerDay>
<DaysToStockOut>0</DaysToStockOut>
<InvalidDataCount>0</InvalidDataCount>
<ValidDataCount>0</ValidDataCount>
<ZeroVolumeCount>0</ZeroVolumeCount>
<ZeroProductLevelCount>0</ZeroProductLevelCount>
<ZeroTotaliserAmountCount>0</ZeroTotaliserAmountCount>
</SiteAlarmDetails>
XML;
$xml=simplexml_load_string($xmlString);
echo (string)$xml->AlertId;//type-casted to string
echo PHP_EOL;
echo (string)$xml->SiteCode;
echo PHP_EOL;
echo (string)$xml->SiteName;

SOAP web service not recognizing ArrayOfAnyType from PHP client for Agemni Database API

I am trying to insert a lead into the SOAP web service with this WSDL: http://www.agemni.com/AgemniWebservices/service1.asmx?WSDL
The API is documented here: http://wiki.agemni.com/4Integration_And_API%27s/APIs/Database_API
This web service's back end code seems to be in .NET.
Here is my code:
// Create soap client.
try {
$this->client = new SoapClient("https://www.agemni.com/AgemniWebservices/service1.asmx?WSDL", array('soap_version' => SOAP_1_2, 'cache_wsdl' => 0, "trace" => true));
} catch (Exception $e) {
$this->logMessage('Couldn\'t connect to web service.');
throw $e;
}
// Create parameters.
$dataParams = new \stdClass;
$dataParams->strUsername = 'userman';
$dataParams->strPassword = 'hissecretpassword';
$dataParams->strCompanyName = 'validcompanyname';
$leadCode = 2;
$dataParams->objecttype = $leadCode;
// Create keys.
$dataParams->keys = new \stdClass;
$dataParams->keys->Phone = 'Phone';
// Create values.
$dataParams->values = new \stdClass;
$dataParams->values->Phone = '8011234567';
// Call web service.
$validateResult = $this->client->CreateEntity($dataParams);
Here is the response I receive:
object(stdClass)#27 (1) {
["ValidateEntityResult"]=>
object(SoapVar)#29 (4) {
["enc_type"]=>
int(0)
["enc_value"]=>
object(stdClass)#28 (10) {
["statusCode"]=>
string(9) "Succeeded"
["status"]=>
string(5) "Error"
["description"]=>
string(56) "Phone number is required and needs to be 10 digits long."
["errorNumber"]=>
int(1)
["xmlResult"]=>
string(39) "<?xml version="1.0" standalone="yes" ?>"
["EntityValidated"]=>
bool(false)
["EntityCreated"]=>
bool(false)
["EntityUpdated"]=>
bool(false)
["EntityIDCreated"]=>
int(0)
["isloggedIn"]=>
bool(false)
}
["enc_stype"]=>
string(15) "ExceptionReport"
["enc_ns"]=>
string(44) "http://tempuri.org/AgemniWebService/Service1"
}
}
I found a similar issue on PHPFreaks that was years old and was never successfully answered. I see other similar issues with nesting parameters in the PHP SoapClient, but all of the suggestions I've seen end in the same error message for me.
The authentication is being received just fine, along with the company name. However, I've stabbed at formatting, parsing, and encoding the keys and values parameters in nearly every way I could conceive. I've played with declaring my parameters as SoapVar and SoapParam with every potential constructor parameter I could think of and still have received the same error message.
I'd greatly appreciate any suggestions as to any reason that someone could suppose that the Agemni web service is not accepting my lead's phone number.
Thanks.
Update
I can just generate the XML request I need in a string and send it using cURL, but I'd like to use SoapClient. However, I have a hard time generating the elements within the keys and values nodes to look like this:
<anyType xsi:type="xsd:string">phone</anyType>'
I followed some guides on creating custom complex types in SoapClient, but I couldn't get it to create XML that looks like that.

PHPmyGraph: send an array with GET

I could not find any answer to my question.
I'm using PhPmyGraph ( http://phpmygraph.abisvmm.nl/ ) to display a graph of some data from my databases.
The problem is that I have to create my arrays in the file itself, and if I want 2 graphs on the page I need to create 2 different files.
Apparently the file is easier to use with a CMS but I'm not using one.
This is the file graph.php:
<?php
//Set content-type header for the graphs
header("Content-type: image/png");
//Include phpMyGraph5.0.php
include_once('../phpMyGraph5.0.php');
//Set config directives
$cfg['title'] = 'Example graph';
$cfg['width'] = 500;
$cfg['height'] = 250;
//Set data
$data = array(
'Jan' => 12,
'Nov' => 78,
'Dec' => 23
);
//Create phpMyGraph instance
$graph = new phpMyGraph();
//Parse
$graph->parseVerticalPolygonGraph($data, $cfg);
?>
I call it in my page index.php:
echo " < img src=\"graph.php\"> ";
Is there another way to do it? And send the data from index.php to graph.php?
Or maybe move the code graph.php into index.php ? The problem is for the image object, I don't really know how to do it!
UPDATE:
I have almost found a solution, my code is now:
in graph.php:
//Parse
$graph->parseVerticalPolygonGraph(unserialize($_GET['data']), $cfg);
index.php :
$select_daily = mysql_query("SELECT * FROM table");
while ($row_daily = mysql_fetch_assoc($select_daily) ){
$y = substr($row_daily['ymd'], 0, -4); // Year
$m = substr($row_daily['ymd'], 4, -2); // Month
$d = substr($row_daily['ymd'], -2); // Day
$key = $d."/".$m."/".$y;
$data_daily [$key] = $row_daily['members'];
}
foreach($data_daily as $key => $value) {
echo $key ,' : ', $value ,'<br/>';
}
echo "< img src=\"graph.php?data=".serialize($data_daily)."\">";
But I get the error "provided data is not an array"
I can't see what's wrong with it?
if I do var_dump($data_daily) I get:
array(8) { ["14/12/2011"]=> string(1) "0" ["13/12/2011"]=> string(2)
"11" ["12/12/2011"]=> string(1) "0" ["11/12/2011"]=> string(1) "2"
["10/12/2011"]=> string(1) "9" ["09/12/2011"]=> string(1) "3"
["08/12/2011"]=> string(1) "6" ["07/12/2011"]=> string(1) "6" }
UPDATE2:
var_dump($data1); gives:
array(12) { ["Jan"]=> int(12) ["Feb"]=>
int(25) ["Mar"]=> int(0) ["Apr"]=> int(7) ["May"]=> int(80) ["Jun"]=>
int(67) ["Jul"]=> int(45) ["Aug"]=> int(66) ["Sep"]=> int(23)
["Oct"]=> int(23) ["Nov"]=> int(78) ["Dec"]=> int(23) }
and var_dump($s_data1 = serialize($data1)) gives:
a:12:s:3:"Jan";i:12;s:3:"Feb";i:25;s:3:"Mar";i:0;s:3:"Apr";i:7;s:3:"May";i:80;s:3:"Jun";i:67;s:3:"Jul";i:45;s:3:"Aug";i:66;s:3:"Sep";i:23;s:3:"Oct";i:23;s:3:"Nov";i:78;s:3:"Dec";i:23;}
Then unserialize($s_data1); gives the same thing than $data1
So the argument 1 of the parse should be correct... I can’t see what is wrong
I finally gave up and loaded my arrays in graph.php:
if ($_GET['data'] == 'daily'){
$cfg['title'] = 'daily';
$graph->parseVerticalPolygonGraph($data_daily, $cfg);
}
And I call the file like that:
echo "<img src=\"graph.php?data=daily\">";
Thanks for your help anyway
I previously needed a page to display multiple graphs using phpMyGraph and the approach I took was to use data URI's and php's ob_start() and ob_get_clean()
Simply use this for each graph:
ob_start();
$graph->parseVerticalPolygonGraph($data, $cfg);
$img = ob_get_clean();
echo "<img src='data:image/gif;base64," . base64_encode($img) . "/>";
I recommend using gif's for the format since that way your page size will not be huge, you can do this by setting $cfg["type"] to "gif" (See here http://phpmygraph.abisvmm.nl/#ConfigDirectives)
This will also reduce the overhead of multiple requests and prevent hotlinking to the images.
You can read more about data URI's here
http://en.wikipedia.org/wiki/Data_URI_scheme
you might want to try
echo "< img src=\"graph.php?data=".urlencode(serialize($data_daily))."\">"
I might be misunderstanding which script is throwing the error, however (I'm presuming that it's graph.php that's giving you the provided data is not an array).
Try using json instead of serialize
echo "< img src=\"graph.php?data=".urlencode(json_encode($data_daily))."\">"
$graph->parseVerticalPolygonGraph(json_decode($_GET['data'],true), $cfg);
I see no reason for this to throw an error.

Categories