XML loading very slow with PHP - php

I have the following code:
<?php
include "convert/xmlToArray.php";
$query_string = $_GET['query_string'];
if ($query_string == "") {
$query_string = "travel";
}
$completeurl = "http://my_site";
$xml = simplexml_load_file($completeurl);
$arrayData = xmlToArray($xml);
echo json_encode($arrayData);
?>
There is another page that fetches theses results with JavaScript and displays it to the user.
The page takes more than 10 seconds to fetch the results and display them.
Does anyone have any idea on how to make it load faster? Is there a client based solution?
Thanks

Save all your data to file php file. For example if you want get array you can simply include a file and use data. Its simply way to using php faster.

Related

Calling images dynamicly using javascript and php

i am new to Javascript and i have created the code below it works fine no problem at all however i want to know what is i want to pull the image dynamically using php and javascript from mysql database how can i refactor my code bellow. thanks in advance for your contribution.
var myimage = document.getelementById("mainImage");
var imageArray =["images/overlook.jpg","images/garden.jpg","images/park.jpg"];
var imageIndex =0;
function changeimage(){
myimage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length){
imageIndex = 0;
}
setInterval(changeimage, 5000);
One of several options.
Query the database for the column with the URL of the images.
$query = mysql_query("SELECT url FROM images");
Then something like this to get an array out of it:
$images = array();
while($row = mysql_fetch_array($query)){
$images[] = $row['url'];
}
Then generate this string (that you use in the Javascript provided):
var imageArray = ["images/overlook.jpg","images/garden.jpg","images/park.
using the array you retrieved from the database. You could use json_encode in PHP for this if you don't want to mess around with error prone string building.
$imagesAsJsonArray = json_encode($images);
Echo it. Done.
Not the most elegant of solutions. But it gives you something to play with. Check out a few PHP tutorials online and you'll soon get the hang of it.
Two choices:
Using PHP when your page is created, put an array of images in the page and use page-level javascript to cycle among them.
Using Ajax in the page, call from the page to the server to get the next image and then use client-side javascript to make that returned image visible on the page.

How to Parse XML data to a PHP variable

I am mediocre with php and ignorant with XML...if you can be detailed it will help me learn.
I am attempting to use PHP programming to execute a scripts to this link... http://ws.geonames.org/postalCodeSearch?postalcode=VARIABLE_ZIP&country=US. The VARIABLE_ZIP is the actual zip code entered into the form that will submit the information in the link above. The output of that link creates an XML page that i do not want displayed anywhere on my website.
What I want to do is capture the XML data Latitude and Longitude values as variables within php and store them into a database.
1) I have a form
2) The user inputs their zip code into the form
3) upon submitting the form: I want code to capture the XML data generated by the link with the zip code variable added: http://ws.geonames.org/postalCodeSearch?postalcode=90210&country=US (I don't want this page to display anywhere, I just want to capture the data)
4) I want to take those variables back to PHP so I can store them in my database
*Step 3 is an unknown process for me.
This is what the XML code generates on a separate page...(i don't want a page to display):
= = = = =
<geonames>
<totalResultsCount>1</totalResultsCount>
<code><postalcode>90210</postalcode>
<name>Beverly Hills</name>
<countryCode>US</countryCode>
<lat>34.09011</lat>
<lng>-118.40648</lng>
<adminCode1>CA</adminCode1>
<adminName1>California</adminName1>
<adminCode2>037</adminCode2>
<adminName2>Los Angeles</adminName2>
<adminCode3/><adminName3/>
</code>
</geonames>
= = = = =
Someone provided me with the following but I am not sure how to execute it and parse the variables I need:
<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];
$xml = file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode={$pc}&country={$c}");
file_put_contents("geopost_{$pc}_{$c}.xml",$xml);
?>
Thank you for your assistance!
The best thing to do is to see how the XML is structured by navigating directly to the link (I used my own zipcode and country), and then navigate through the tree grabbing the data you need using SimpleXMLElement. This example grabs the Latitude and Longitude:
<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];
$xml = new SimpleXMLElement(file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode=".$pc."&country=".$c));
$lat = $xml->code->lat;
$lng = $xml->code->lng;
echo "Lat: $lat<br/>Lng: $lng";
?>
This should help get you started with storing those values.
Step 3 is to process the xml with PHP which is outlined quite nicely with these Simple XML examples
Take a look at PHPs SimpleXML for how to parse and read the XML data received.

How do I make a script to grab certain piece of data from a JSON API?

I'm trying to build a little script that would let me do this:
http://example.com/appicons.php?id=284417350
and then display this in plain text
http://a3.mzstatic.com/us/r1000/005/Purple/2c/a0/b7/mzl.msucaqmg.png
This is the API query to get that information (artworkUrl512):
http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=284417350
Any help and example code would be much appreciated!
I am not sure why you have jQuery in your tags, unless you want to make the request dynamically without a page refresh. However you can do this simply in PHP using the following example:
$request = array (
"app_id" => #$_GET["id"]
);
// parse the requests.
if (empty($request["app_id"])) {
// redirects back / displays error
}
else {
$app_uri = "http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsLookup?id=" . $request["app_id"];
$data = file_get_contents ($app_uri);
$json = json_decode (trim($data));
print($json->results[0]->artworkUrl100);
}
$request = file_get_contents($itms_url);
$json = json_decode(trim($request));
echo $json[0]->artworkUrl512;
should work in PHP. Unless of course there is more than one hit to the search. A solution using jQuery is probably not very much more difficult.

How to properly backup results to the database?

I'm working on a jQuery AJAX call where I get a set of results from a search. I return html with an echo and I need to backup the results by inserting into the db. Will also have to check if they already exist before inserting.
I want the html to be returned as soon as possible. If I add insert code below the echo will the entire code have to finish running before the html is returned?
The most important thing is content returning back to the user right away.
This is all on mobile so every 100 milliseconds count.
$data = file_get_contents($url);
$result = json_decode($data, true);
foreach ( $result->results as $items ) {
$name = $items->name;
$description = $items->desc;
$id = $items->id;
$coverurl = $items->coverurl;
$returnhtml .= "<h3>".$name."</h3>";
$returnhtml .= "<h4>".$description."</h$>";
}
echo $returnhtml;
//how to backup to database
//check if already in db
//insert into db
To answer your question, yes. The entire script must complete before the HTML is outputted back to the client. You can use output buffering to capture the output, send it, and then continue on with other processing. The server will then try to output the remaining when the script finished but AJAX doesn't know how to handle the second part so it just ignores it.
Look into http://www.php.net/manual/en/ref.outcontrol.php
It'll be something like:
ob_start();
... generate html for client
ob_flush();// send output buffer to the client
... insert data into database
ob_end_clean();
After do the ob_flush as explained before, you can try MySQL INSERT DELAYED if you don't wait for the INSERT to complete.
http://dev.mysql.com/doc/refman/5.5/en/insert-delayed.html

how to pass values from a php script to another dynamically?

i want to pass a url value from a php script to another.I have a database in which i have stored some feeds.I have given some weight values to these feeds and a php script grabs a feed url randomly based on their weights.I want to take the feed url which has been grabbed by the script and then pass this url in another php script where it will be parsed with simplepie in order to show its content.
I am posting the codes of the two files right here:
this is the first script which grabs randomly the feed
http://pastebin.com/2ciQ87Es
this is the second script in which i want to pass the value and makes the parsing of the feed
http://pastebin.com/eN5qG29e
Have you something to recommend??
thanks in advance
Would a $_SESSION not suffice?
In the first script:
session_start();
$_SESSION['session_name'] = 'value';
In the second script:
session_start();
print $_SESSION['session_name'];
On second thoughts, could you not pass the value in a query string, to the second page.
second-page.php?key=value
You could wrap grabbing the feed url from the database in a function, and just include that file like any other php file, and then call that function.
//// feedgrabber.php
<?php
function grabber(){
$query = "SELECT * FROM `feeds`";
//takes all the feed that are declared
$result = mysql_query($query);
$data = array();
while($output = mysql_fetch_assoc($result)) {
$data[] = $output; // assigns feeds to an array called $data, one after the other, in they go!
}
return randomchoice($data); // finds a random feed by calling the function
}
?>
and then on the page where you need it:
require('feedgrabber.php');
$feed = grabber();

Categories