loading a xml file instead of download - php

Hey guys I have stored few xml files in database and want to get them for xslt transformation. But I am unable to load it for transformation, they get downloaded to PC. I want to process it instead of download.
here is my code: I am accessing the file with its id.
// Fetch the file information
$query = "
SELECT `pname`,`pdata`
FROM `plist`
WHERE `pid` = {$id}";
$result = $dbLink->query($query);
if ($result) {
// Make sure the result is valid
if ($result->num_rows == 1) {
// Get the row
$row = mysqli_fetch_row($result);
echo "$row[0]";
header("Content-Type:text/xml");
header("Content-Disposition: inline");
echo $row['pdata'];
}
mysqli_free_result($result);
---missing something here to get file data--
until now it gets downloaded but i want to continue the transformation
$xml = new DOMDocument;
$xml->load(what goes here??);
$xsl = new DOMDocument;
$xsl->load('file.xslt');
//etc
any help appreciated.

If you have a string with the XML then you can use $xml->loadXML($row['pdata']) I think to parse the XML.

Working solution that i found later enjoy..:)
<?php
if (isset($_GET['id'])) {
// Get the id
$id = intval($_GET['id']);
// Make sure the name is in fact a valid
if ($id <= 0) {
die('The id is invalid!');
} else {
// Connect to the database
include 'config.php';
}
// Fetch the file information
$query = "select id, name, data from data_table where id ={$id};";
$resultx = $mysqli->query($query) or die($mysqli->error . __LINE__);
$arr = array();
if ($resultx->num_rows > 0) {
while ($row = $resultx->fetch_assoc()) {
$arr[] = $row['tdata'];
}
}
$string = implode(",", $arr);
// Load XML file
$xml = new DOMDocument; // create new document
$xml->loadXML($string); // load string to it

Related

Android: Get details from php script with htaccess and htpasswd

I have a php script with this output:
And then I display this data in Android. But now, I put a .htaccess and .htpasswd file in the directory and I have to enter a password, if I want to read the data. My question is how can I display the data in Android, now with the files (.htaccess and .htpasswd). Is there a way to enter the password in Android to show the data because now I cant display the data..
Thanks
EDIT:
This is my php script:
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT * FROM users") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["feed"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$feed = array();
$feed["uid"] = $row["uid"];
$feed["unique_id"] = $row["unique_id"];
$feed["name"] = $row["name"];
$feed["full_name"] = $row["full_name"];
$feed["email"] = $row["email"];
$feed["age"] = $row["age"];
$feed["about_the_customer"] = $row["about_the_customer"];
$feed["encrypted_password"] = $row["encrypted_password"];
$feed["salt"] = $row["salt"];
$feed["created_at"] = strtotime($row["created_at"]);
$feed["updated_at"] = $row["updated_at"];
$feed["imageName"] = $row["imageName"];
// push single product into final response array
array_push($response["feed"], $feed);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No feed found";
// echo no users JSON
echo json_encode($response);
}
?>

Export data into an existing csv file using php

I'm trying to get data from my data base and load it into an existing csv file i get no errors but each time i check with the file it stills empty.
this is my PHP file :
<?
$server="XXXX";
$user="XXXX";
$password="XXXX";
$db="XXXX";
mysql_connect($server,$user,$password) or die('erreur au serveur');
mysql_select_db($db) or die('erreur db');
if ($file=fopen('XXXX/toERP/exportfactures.csv','w')!=FALSE){
$req='select * from facture';
$sql = mysql_query($req);
$cpt=1;
$response["factures"] = array();
$output=fopen('http://XXXXX/toERP/exportfactures.csv','w');
if (mysql_num_rows($sql) > 0) {
// looping through all results
// factures node
while ($row = mysql_fetch_array($sql)) {
// temp user array
$facture = array();
$facture["id_fac"] = $row["id_fac"];
$facture["id_client"] = $row["id_client"];
$facture["id_agent"] = $row["id_agent"];
$facture["montant"] = $row["montant"];
$facture["type_paiement"] = $row["type_paiement"];
$facture["id_prod"] = $row["id_prod"];
$facture["date_vente"] = $row["date_vente"];
// push single client into final response array
array_push($response["factures"], $facture);
}
foreach ($response["factures"] as $ligne){
var_dump($ligne);
fputcsv($output,$ligne) or die ('erreur');
echo $cpt."inserted";
$cpt++;
}
fclose($file);
} else {
echo "file not found";
}
}
?>
You are unable to write to an http location try changing it to a local directory.
fopen('\XXXX\toERP\factures.csv','w')
or just change the save location
$output = fopen('\XXXX\toERP\factures.csv','w');
fputcsv($output, explode(';',$ligne));
You are trying to open a file from remote server due to url example your have wrote. This is your most possible issue if you see the lines inserted by echo.
Here is the definition for fputcsv : http://php.net/manual/en/function.fputcsv.php
As the second argument, you need to provide an array with fields.
$ligne in your case, is an array with one element: ['factures'] which is another array, so you need to change the foreach to match the form below :
foreach ($response['factures'] as $ligne)
Also, as others mentioned: you need to select a local resource for the filename, and make sure you have the right to write on it.
this code worked for me :
<?
$server="mysql12.000webhost.com";
$user="a2258793_star";
$password="star123";
$db="a2258793_stores";
$con = mysql_connect($server,$user,$password) or die('erreur au serveur');
mysql_select_db($db) or die('erreur db');
mysql_select_db($db);
$file='toERP/factures.csv';
$fp = fopen($file,"w");
$sql = mysql_query("select * from facture") or die (mysql_error);
mysql_data_seek($sql,0);
while ($row = mysql_fetch_assoc($sql)){
$sep = "";
$comma = "";
foreach ($row as $name => $value)
{
$sep.=$comma.''.str_replace('','""',$value);
$comma =";";
}
$sep.="\n";
fputs($fp,$sep);
}
fclose($fp);
?>

PHP file_get_contents foreach

I've found an existing script on the internet that grabs data from WHOIS servers and extracts the relevent data (Eg. Expiry date, Status). As it was long abandoned I have been modifying it and created my own script.php?id=domain.com which lets me enter any domain and whois data comes up,
What I'm having trouble with is I have my whois.php file which I want to grab a list of domains out of my MySQL database and attempt to extract the Expiry/Status data of each domain using (file_get_contents to my script.php and foreach) then update the database with the relevant information.
I'm fairly certain I have coded everything apart from the "Foreach" & "File_get_contents" part right therefore my script encounters errors.
"Warning: Invalid argument supplied for foreach() in /home/user/public_html/mydomain.com/domains/whois.php on line 39"
is the error I receive.
Snippet of my whois.php:
include("database.php");
// Select one domain from the database that hasn't been checked yet
$sql = "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$domain = $row[0];
if(mysql_num_rows($result) == 0){
die("No domains found in the database.");
}
// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt = substr($domain, -3); // grab the domain extension
//var_dump($whois);
$arr = array($content);
foreach($arr as $id) {
echo $id, '<br>';
$data = file_get_contents('http://codestrike.net/domains/script.php?id='.$domain.'');
echo $data, '<br>';
}
foreach($data as $whoisline){
if(strstr($whoisline,"Expiration")){
$whoisline = str_replace("Expire Date:","",$whoisline);
$whoisline = trim($whoisline);
$expiration = substr($whoisline,0,11);
}
if(strstr($whoisline,"Status")){
$statusline = $whoisline;
}
}
$status = str_replace("Status:","",$statusline);
$status = trim($status);
Script.php?id=domain.com works fine, its just a matter of having the whois.php finding the expiry date/status for each domain out of my MySQL Database.
Cheers.
Change:
$data = file_get_contents('http://mydomain.com/domains/script.php?id='.$domain.'');
to:
$data = file('http://mydomain.com/domains/script.php?id='.$domain);
file_get_contents returns the entire file as a single string. file splits it up into an array, where each element is a line of the file.
You also need to process $data in the first foreach loop. Otherwise, you're just overwriting $data each time through the loop, and the code that uses it just gets the last one.
include("database.php");
// Select one domain from the database that hasn't been checked yet
$sql = "SELECT domainName from domains WHERE 1 ORDER BY lastChecked ASC";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$domain = $row[0];
if(mysql_num_rows($result) == 0){
die("No domains found in the database.");
}
// Grab the WHOIS information for the domain selected
// ---------------------------------------------------------------
$domainExt = substr($domain, -3); // grab the domain extension
//var_dump($whois);
$arr = array($content);
foreach($arr as $id) {
echo $id, '<br>';
$data = file('http://mydomain.com/domains/script.php?id='.$domain);
var_dump($data); echo '<br>';
foreach($data as $whoisline){
if(strstr($whoisline,"Expiration")){
$whoisline = str_replace("Expire Date:","",$whoisline);
$whoisline = trim($whoisline);
$expiration = substr($whoisline,0,11);
}
if(strstr($whoisline,"Status")){
$statusline = $whoisline;
}
}
$status = str_replace("Status:","",$statusline);
$status = trim($status);
}

Unable to get data in proper format

I am unable to parse data in PHP from MySQL.
Here's my code
<?php
header('Content-Type: text/html; charset=utf-8');
// array for JSON response
$response = array();
echo 'भगवान';
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM create_event") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["create_event"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["id"];
$product["desc"] = $row["desc"];
$text;
array_push($response["create_event"], $product);
}
// success
$response["success"] = 1;
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
It's my database, I have set all the collation in utf-8 ci format but still not working:
I have tried all possible solutions and help online and have gone through popular posts and answers and also have set browsers settings to support Hindi lang but still displaying ? marks in output. Here is the output format
भगवान{"id":"1","desc":"???? ??????"}
mysql_query('SET character_set_results=utf8');
Use this after connecting to database or before using any select statement.

testing php function with poster extention

I made a simple API to get the data from the database to be viewd at android app
I have a Problem testing my php functions
example of the functions:-
<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];
// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["city"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
// Full texts id dead_name masged_name gender salah notes date city_name hour min
$region_subscribt = array();
$region_subscribt["id"] = $row["id"];
$region_subscribt["city_name"] = $row["city_name"];
$region_subscribt["region_name"] = $row["notes"];
$region_subscribt["notes"] = $row["country_name"];
// push single product into final response array
array_push($response["city"], $region_subscribt);
}
// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;
// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function
// get_city();
?>
when I using poster extention with firefox and put at the content , I got no data
when I put at the content
getcity();
but when I call the function inside the php file I got the data correctly !
How to pass the values at the httprequest to use the returned json at another app !
You need to send the method name as a parameter not as a content and then check the method name and call the code you need.
see this snapshot
if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
get_city();
}
you also need to set the content type as following
header('Content-type: application/json');
your final code will be something like this
<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];
// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());
header('Content-type: application/json');
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["city"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
// Full texts id dead_name masged_name gender salah notes date city_name hour min
$region_subscribt = array();
$region_subscribt["id"] = $row["id"];
$region_subscribt["city_name"] = $row["city_name"];
$region_subscribt["region_name"] = $row["notes"];
$region_subscribt["notes"] = $row["country_name"];
// push single product into final response array
array_push($response["city"], $region_subscribt);
}
// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;
// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function
if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
get_city(); }
?>
and here is some very good and pretty easy tutorial summarize the whole precess
Create a Basic Web Service Using PHP, MySQL, XML, and JSON

Categories