I need to store the XML that i get it from Google Analytics. Its format is XML file. I need to create the script ( PHP ) that will read XML file from Google Analytics and store in my server with user defined name. I tried like that
<?php
$dom = new DOMDocument();
$dom->load('https://www.google.com/analytics/reporting/export?fmt=1&id=346044461&pdr=20100611-20100711&cmp=average&rpt=DashboardReport');
$dom->save('books3.xml');
?>
Can you help me
you're not assigning the result of load to anything you can save afterwards. and that is assuming you created a function load.
you'd need something more along the lines of
<?php
$remoteUri = 'https://www.google.com/analytics/reporting/export?...';
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($remoteUri));
$xml = $doc->saveXML($doc->documentElement);
file_put_contents($yourLocalFilePath, $xml);
or if you just want a completely verbatim copy locally:
<?php
$remoteUri = ...
file_put_contents($yourLocalFilePath, file_get_contents($remoteUri));
the second, simpler version doesn't attempt to parse any xml and will therefore not have any clue if something is wrong with the recieved document.
depending on your server, you might have to resort to more complex methods of getting the file if url wrappers for fopen aren't enabled, or if your google endpoint wants to use cookies etc. for example.
Related
I have a response that I got from a server using PHP, it is returned in XML format. Unfortunately I cannot disclose the code that is in question since it is for a client, but here is a shortened version of it:
// server request code here…
$result = $soap>__doRequest($xmltosend,URL,$action,1); //the final part of the request process…
// $result is the returned xml that is to be converted into a separate file
Is there any way that I could take the returned XML and make a new file? If you have any questions I will be more than happy to answer them!
Thanks in advance!
Try it with DOMDocument:
$doc = new DOMDocument();
$doc->loadXML($result);
$doc->save("/tmp/your-document.xml");
You can find more on this in the php documentation
I am trying to find a way of displaying the text from a website on a different site.
I own both the sites, and they both run on wordpress (I know this may make it more difficult). I just need a page to mirror the text from the page and when the original page is updated, the mirror also updates.
I have some experience in PHP and HTML, and I also would rather not use Js.
I have been looking at some posts that suggest cURL and file_get_contents but have had no luck editing it to work with my sites.
Is this even possible?
Look forward to your answers!
Both cURL and file_get_contents() are fine to get the full html output from an url. For example with file_get_contents() you can do it like this:
<?php
$content = file_get_contents('http://elssolutions.co.uk/about-els');
echo $content;
However, in case you need just a portion of the page, DOMDocument and DOMXPath are far better options, as with the latter you also can query the DOM. Below is working an example.
<?php
// The `id` of the node in the target document to get the contents of
$url = 'http://elssolutions.co.uk/about-els';
$id = 'comp-iudvhnkb';
$dom = new DOMDocument();
// Silence `DOMDocument` errors/warnings on html5-tags
libxml_use_internal_errors(true);
// Loading content from external url
$dom->loadHTMLFile($url);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
// Querying DOM for target `id`
$xpathResultset = $xpath->query("//*[#id='$id']")->item(0);
// Getting plain html
$content = $dom->saveHTML($xpathResultset);
echo $content;
Anybody any idea how they do it? I currently use OffLiberty.com to parse Mixcloud links to get the raw MP3 URL for use in a custom HTML5 player for iOS compatibility, I was just wondering if anyone knew how exactly their process works, so I could create something similar that would 'cut out the middleman' so to speak, so my end-user wouldn't have to go to an external site to get a link to the MP3 for the mix they want to post. Just a thought really, not terribly important if it couldn't be done, but it would be a nice touch :)
Anybody any idea?
Note that I'm against content scraping and you should ask those website permission to scrap their MP3 URLs. Else, if I was them, I'd block you right now and ad vitam æternam.
Anyway, you can parse its HTML using DOMDocument.
For example :
<?php
// just so you don't see parse errors
$internal_errors = libxml_use_internal_errors(true);
// initialize the document
$doc = new DomDocument();
// load a page
$doc->loadHTMLFile('http://www.mixcloud.com/LaidBackRadio/le-motel-on-the-road/');
// initialize XPATH for the document
$xpath = new DomXPath($doc);
// span with "data-preview-url" seems to contain MP3 url
// we request them inside a DomNodeList http://www.php.net/manual/en/class.domnodelist.php
$mp3 = $xpath->query('//span[#data-preview-url]');
foreach($mp3 as $m){
// we print the attribute value
echo $m->attributes->getNamedItem('data-preview-url')->nodeValue . '<br/>';
}
libxml_use_internal_errors($internal_errors);
I am building an iphone app which allows people to update an xml file by sending a POST to a php script. After they send the post to the php script and the xml is updated, I would like the user to be able to cancel the update to the XML. How can I delete just one element from the XML and rewrite the XML file to the same location on the server (in other words, just the one element is now gone, everything else is the same)? To add an element I used code that looks like the following:
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xml = new SimpleXMLElement($xmlStr);
$bartenders = $xml->xpath('//Bartenders');
$new_bartender = $bartenders[$newBar_ID]->addChild('Bartender');
$new_bartender->fname = $newfname;
$new_bartender->lname = $newlname;
$new_bartender->imageURL = $newimageURL;
$new_bartender->shift = $newShift;
print_r($bartenders);
$xml->asXML('Bars.xml');
When I send the POST method to the php script, I have the element's attribute to identify which is to be deleted.
Thanks for your help.
Possible duplicate of THIS.
From your code snippet I don't really know what data is given. You could try to do either
unset($bartenders[$newBar_ID]);
or
$bartenders->removeChild($bartenders[$newBar_ID]);
I guess... I wrote these based on code snippets google turned up, never tested them. Feel free to give me feedback if it works or not.
I'm new in PHP. I want to read OPDS catalog (ex : http://feedbooks.com/catalog.atom) and then store the catalogs into json file.
Could someone suggest me how to do it, or point me to usefull article/source code to do it?
Open the URL (allow_url_fopen must be switched on for that, otherwise take a look at curl)
$content = file_get_contents('http://feedbooks.com/catalog.atom');
$xml = new SimpleXMLElement($xmlstr);
print_r($xml);
the print_r will show you the object you received, alter the object as you like and create a JSON via json_encode($xml), and write it to a file.
Alternatively you can use a Yahoo Pipe to read the feed and output it as json.
$json = file_get_contents('http://pipes.yahoo.com/pipes/pipe.run?_id=29ec5982a8bede87d83a402f7f8ac0ec&_render=json');
That's the actual pipe: http://pipes.yahoo.com/pipes/pipe.run?_id=29ec5982a8bede87d83a402f7f8ac0ec&_render=json