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

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";
}

Related

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;

Getting a list of Contacts from Acumatica Web Services API using PHP

I'm trying to use the Acumatica Web Services API to get a list of Contacts (really I'm looking to get ANYTHING, but Contacts are what I'm playing with right now).
I'm successfully able to get a SoapClient connected, but not sure what exactly to do from there to pull a list of all Contacts.
Seeing how you didn't specify Acumatica version or Webservices I'm assuming you are trying to use the "Screen WebAPI" that was in 5.2 and earlier and not the new "Contract Based API" in 5.3
With that in mind, here is an example of how to make the connection and retrieve a list of all of the contacts.
The first step is to utilize the "acuwsdl2php" helper file to generate the needed screen helper classes for PHP.
In the case of contacts:
php acuwsdl2php.php {url of your site}/Soap/CR302000.asmx?WSDL CR302000
This will create the CR302000 subfolder with a Screen.php file that is the php equivalent of the schema for the screen.
Second, here is a sample class that retrieves contact information
<?php
require_once('AcumaticaGate.php');
$client = new AcumaticaGate('{user}', '{password}', 'CR302000','{site}/Soap/');
$Contact_summary = $client->Schema->GetSchemaResult->ContactSummary;
$Contact_detailsummary = $client->Schema->GetSchemaResult->DetailsSummary;
$every_Contact = $Contact_summary->ServiceCommands->EveryContactID;
$Contact = $Contact_summary->ContactID;
$Contact_fname = $Contact_detailsummary->FirstName;
$Contact_lname = $Contact_detailsummary->LastName;
$export_param = new Export();
$export_param->commands = array($every_Contact, $Contact, $Contact_fname, $Contact_lname);
$export_param->filters = array();
$export_param->breakOnError = false;
$export_param->includeHeaders = true;
$export_param->topCount = 0;
$export = $client->Client->Export($export_param);
print_r(var_dump($export));
The output here is something like this:
[177]=>
object(stdClass)#562 (1) {
["string"]=>
array(3) {
[0]=>
string(3) "358"
[1]=>
string(4) "Anna"
[2]=>
string(7) "Johnson"
}
}
[178]=>
object(stdClass)#563 (1) {
["string"]=>
array(3) {
[0]=>
string(3) "359"
[1]=>
string(4) "Yona"
[2]=>
string(5) "Jones"
}
}
The acuwsdl2php and AcumaticaGate files are helper files that Acumatica provided to partners. They might also be available on the client portal for download. A quick google for them though and I believe you can find them on a few public sites.
As a side note, these helper files were originally written for 4.x. You should look at the 5.x guides (assuming you have 5.x) for added information on logging off of a webapi when finished calling it.

Cannot get the value of the xml fields

Hi i have this xml response that i parse and i can access the third text field, i have parsed it and i even do a var_dump($xmlObj->TerminalCommandResponse->Text); in which i get on screen
object(SimpleXMLElement)#48 (14) {
[0]=> string(4) "BB"
[1]=> string(45) " *** BEST QUOTATION ***"
[2]=> string(52) " FOR THIS ITI"
[3]=> string(48) " *** BF SEGMENTS 1P/2P ***"
...
}
But when i try to directly access:
$XMlText=$xmlObjFourth->TerminalCommandResponse->Text;
var_dump($XMLText[2]);
It doesn't show anything. I even tried a foreach loop in case i get the keys wrong but still the same issue
<terminal:TerminalRsp xmlns:terminal="terminal_v50_0" TransactionId="F09006B80A0759BF61F85144F306F735" ResponseTime="527">
<terminal:TerminalCommandResponse>
<terminal:Text>BB</terminal:Text>
<terminal:Text>*** BEST QUOTATION ***</terminal:Text>
<terminal:Text>FOR THIS ITI</terminal:Text>
<terminal:Text>*** BF SEGMENTS 1P/2P ***</terminal:Text>
<terminal:Text> PSGR PSG DES </terminal:Text>
<terminal:Text>FQG 1 PY2PC 3640 6201 ADT </terminal:Text>
<terminal:Text> GUARANTEED A </terminal:Text>
<terminal:Text>)><</terminal:Text>
</terminal:TerminalCommandResponse>
</terminal:TerminalRsp>
Probably it is a special character or a whitespace that is blocking you, it is an interesting issue here what i think will help for starters
foreach($XMLText as $k=>$tmp)
{
var_dump(preg_replace("/[^a-zA-Z0-9\s+]+/", "", $tmp));
}
this way you can see whats in every field in the XMLText array

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.

php xml function requirements

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.

Categories