XML Address Trouble - php

I have this xml that I want to insert some info, the code is working fine but when it comes to the address of the XML I'm having some trouble.
I have a link in other page with:
produtoadicionado.php?page=adicionar&cod_produto=1&id=1&nome_produto=Arroz
What I want is complete the name of the XML with the value of $id before. Which in this case is 1.
So it will call and add in: 1_produtos.xml
But is not working. Is creating a new xml named $id_produtos.xml
produtoadicionado.php
<?php
$page = $_GET["page"];
$cod_produto = $_GET["cod_produto"];
$id = $_GET["id"];
$nome = $_GET["nome_produto"];
if ($page == 'adicionar')
{
$xml = simplexml_load_file('$id_produtos.xml');
$produto = $xml->addChild('produto');
$produto->addChild('nome', $nome);
$produto->addChild('cod', $cod_produto);
file_put_contents('$id_produtos.xml', $xml->asXML());
}
?>
Please help me!

Change these lines:
simplexml_load_file('$id_produtos.xml');
file_put_contents('$id_produtos.xml', $xml->asXML());
into this:
simplexml_load_file("{$id}_produtos.xml");
file_put_contents("{$id}_produtos.xml", $xml->asXML());
Please, note the use of double quote instead of the single quote which let PHP to interpret the vars name and replace that with their value. You can read more here.

Related

i want to replace the node value in my XML

This is my xml code.
I want to replace value of End,
for example, i.e, End = 'enddate' ;
End = 'somedate' ;
Please help
<SailingDateRange MaxDuration="P07D" MinDuration="P07D" End="enddate" Start="startdate"></SailingDateRange>
I also tried the below code
$sail_dat_to = $_GET['date'];
$xml->SailingDateRange->End = $sail_dat_to;
But it adding a new line rather than replace the value.
pLease help
$xml = simplexml_load_file('file.xml');
$xml->SailingDateRange['End'] = 'somedate';
$xml->asXml('file.xml');
Read more: http://php.net/manual/en/simplexml.examples-basic.php

append a variable after a link in php

I'm trying to append my variable that displays the count after my links, but I already have code I need replacing links to redirect them to a redirect page.
This is my code currently:
$this->post['message'] = str_replace('href="', 'href="./out.php?link=', $this->post['message']);
$this->post['signature'] = str_replace('href="', 'href="./out.php?link=', $this->post['signature']);
So all the links get redirected to out.php
(small additional question how can i make this not effect img href's?)
So back on topic, what I'm trying to do is add $someVariable[value] after my links, also not effecting images.
The variable would show plain text (numbers)
and for example the end result would look like:
Google(##)
where ## represents $someVariable
DOM confuses me, and I cant think of any other way to do it, so I need some help.
1
$ur = "./out.php";
$k = str_shuffle("7dfg9bf09cv04e79507e197a1dtdy8j3");
$web = $ur."/?link=".$k;
echo $web;
Here is an example.
2
Escaping quotation marks in PHP
<?php
$ur = "./out.php";
// $k = $this->post['message'];
$k = "hello";
$web = "/?link=".$k;
$li = "".$k."(".$k.")";
echo $li."\n";
?>

Get attribute with file_get_contents PHP

I have a sql query that I store in a variable and I displayed. I get the contents of this with file_get_contents from another file, I would like to recover some of this code (which is html) in order to make link. More precisely retrieve the id.
My api.php
$base = mysql_connect ('localhost','root','');
mysql_select_db('administrations', $base);
if(isset($_GET['cp']))
{
$sql = 'SELECT NOM_organisme, ID_organisme
FROM organismes
WHERE code_postal LIKE "%'.$_GET['cp'].'%"
ORDER BY NOM_organisme;';
$req = mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br />'.mysql_error());
}
while ($data = mysql_fetch_array($req))
{
echo '<p id="'.$data['ID_organisme'].'"'.
$data['NOM_organisme'].'</br>'.
$data['ID_organisme'].'</p></br>';
}
I want to get the id="I WANT THIS".
And my index.php (part of my code that retrieves the contents).
if(isset($_POST['cp']))
{
$api = "http://mywebsite.fr/api.php?cp=".$_POST['cp'];
$var = file_get_contents($api);
echo $var;
}
How can I get the id="" in my index.php ?
please look at php get documentation. you need to link to your script with url parameters and access them in your php code.
http://php.net/manual/en/reserved.variables.get.php
echo ''.$data['NOM_organisme'].'</br>'.$data['ID_organisme'].'</br>';
php
if(isset($_GET['id']))
{
$api = "http://mywebsite.fr/api.php?cp=".$_GET['id'];
$var = file_get_contents($api);
echo $var;
}
if you dont want to use url parameter you can use post values
http://php.net/manual/en/reserved.variables.post.php
I understand what your trying to do, but dont find it logical without knowing the purpose of this tiny code :)
Do you have a link or some sort?
Basicly what i should do is:
$base = mysql_connect ('localhost','root','');
mysql_select_db('administrations', $base);
if(isset($_POST['cp']))
{
$sql = 'SELECT NOM_organisme, ID_organisme FROM organismes WHERE code_postal LIKE "%'.$_GET['cp'].'%" ORDER BY NOM_organisme;';
$req = mysql_query($sql) or die('SQL Error !<br>'.$sql.'<br />'.mysql_error());
while ($data = mysql_fetch_array($req))
{
echo '<p id="'.$data['ID_organisme'].'"'.$data['NOM_organisme'].'</br>'.$data['ID_organisme'].'</p></br>';
}
} else {
echo 'show something else';
}
If I get you correctly, you are
Sending a GET request in index.php using file_get_contents() to your website.
The website (api.php) performs an SQL query and prints the result in HTML.
index.php takes this HTML output and stores it in the variable $var.
You want to retrieve all values contained inside the id attribute of the paragraph.
In this case, you probably want to use regular expressions. preg_match_all seems to be appropriate. It should work for you like this:
$out = array();
preg_match_all("/id=\"([^\"]*?)\"/U", $var, $out);
foreach ($out as $value) {
echo 'I found some id ' . htmlspecialchars($out[$value][2]) . '<br />';
}
And additionally:
A decent HTML parser would be much more appropriate in this case (eg. it would not match id="X" in flow text).
Your PHP code is vulnerable to SQL injections.
You should sanitize plain text to HTML appropriately.
First of all, you should try to display your API reply as a JSON-string, this is much more convenient.
If you still want to use your api.php, you first need to close your opening paragraph! You did forget a '>'!
echo '<p id="'.$data['ID_organisme'].'">'.
$data['NOM_organisme'].'</br>'.
$data['ID_organisme'].'</p></br>';
Then you need to parse your paragraph.
You can do it like that:
if(isset($_POST['cp']))
{
$api = "http://mywebsite.fr/api.php?cp=".$_POST['cp'];
$var = file_get_contents($api);
preg_match("#<p id='(.*)'#", $var, $matches);
id = $matches[1];
echo $id;
}

how to pass variable value in URL via file_get_contents

I want to integrate SMS to my API and try to pass the variables in URL but it says error, no number found.
Please help
$sender = "9999708993";
$reply_message = "This is an auto generated message";
echo $sender;
echo $reply_message;
$lines = file_get_contents('http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender');
echo "<pre>$lines</pre>";
use double quotes rather than single quotes if you want to put variables in:
$lines = file_get_contents("http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=myid&password=mypassword&senderid=mysenderid&message=$reply_message&number=$sender");
Please try this:
$sender = "9999708993";
$reply_message = urlencode("This is an auto generated message");
//I think you need to change these
$loginid='myid';
$mypassword='mypassword';
$mysenderid='mysenderid';
$uri = "http://sms2.oxyzen.in/httpapi/sendsms.php?loginid=$myid&password=$mypassword&senderid=$mysenderid&message=$reply_message&number=$sender";
var_dump($uri);
$lines=file_get_contents($uri);
var_dump($lines);
try cleaning the URL, it might have a new line at the end
trim("$URL");

Getting data from facebook xml

I have a problem, I'm trying to get some data for a unique link to my site.
When people are viewing eg: video.php?id=23 i want the script to get the data for that site using $_GET['id'].
Here's my script, and I've tried everything. Hope you can help me!
<?php
$vidurl = $_GET['id'];
function fb_count() {
global $fbcount;
$facebook = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.fniis.dk/video.php?id=$vidurl');
$fbbegin = '<share_count>'; $fbend = '</share_count>';
$fbpage = $facebook;
$fbparts = explode($fbbegin,$fbpage);
$fbpage = $fbparts[1];
$fbparts = explode($fbend,$fbpage);
$fbcount = $fbparts[0];
if($fbcount == '') { $fbcount = '0'; }
}
fb_count();
?>
The problem is that it wont let me print the $vidurl, it doesnt seem to work, because it is only getting data from the following link : fniis.dk/video.php?id= and not eg: fniis.dk/video.php?id=123
You have a couple of problems in your code.
First, you won't be able to access $vidurl in fb_count() unless you specify it as global inside fb_count():
global $vidurl;
It is recommended that you pass $vidurl as a parameter fb_count() instead of using global.
Second, your concatenation of $vidurl in file_get_contents is incorrect. You should be using double quotes instead of single quotes so $vidurl will be processed by PHP. It also wouldn't hurt to use urlencode() here:
// note: using single quotes here and just concatenating with "."
$facebook = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.fniis.dk/video.php?id=' . urlencode($vidurl));
That should get your code working.

Categories