StevenOliver Posted December 2, 2018 Share Posted December 2, 2018 (edited) I envision running code in the background without slowing my own PHP page render. This code accesses optional "Additional Information" via a 3rd party server API ("www.example.com/Additional_Information.php?SKU=1234567"). Unfortunately, it takes a full 10 seconds to retrieve the data from the 3rd party server, and, not all visitors will need this "Additional Information." My own server is fast -- it retrieves mySQL data from my server and renders the result to the Visitor in less than 4 milliseconds. Because it takes an average Visitor about 10 seconds to actually READ my entire page (description, photos, etc.), this would be a perfect time to run a script in the background so that if the Visitor chooses to click the "Additional Information" link, the data will already be available (because the background script would run in the background and post its results to my own mySQL server while the Visitor was busy reading). I've been studying asynchronous cURL calls, opening sockets, forking cURL, and even using cURL to call another cURL script on my server. These seem to slow down my PHP page render. Because not all visitors will want this "Additional Information," I don't want to slow my webpage down for everyone. Is there a way to have code 'truly run in the background' and post its result to my super-fast mySQL server, so if a Visitor does click on the "Additional Information" link, they'd get instant results from my own server? Edited December 2, 2018 by StevenOliver Original question revolved around cURL. I'm trying to "step out of the box" with the awareness there are other methods than cURL. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 2, 2018 Share Posted December 2, 2018 I would kick off an AJAX request on page load to call the curl request then return the results Example <?php /** * IF AJAX CALL, PROCESS */ if (isset($_GET['ajax'])) { $data = get_data('http://www.geoplugin.net/json.gp?ip=19.117.63.253'); exit($data); } /** * curl call * * @param string $path cUrl url */ function get_data($path){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$path); curl_setopt($ch, CURLOPT_FAILONERROR,1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $retValue = curl_exec($ch); curl_close($ch); return $retValue; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Example ajax/curl</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $.get( "", {"ajax" : 1 }, function(resp) { var txt = "Country " + resp["geoplugin_countryName"] + "<br>Latitude " + resp["geoplugin_latitude"] + "<br>Longitude " + resp["geoplugin_longitude"] $("#curloutput").html(txt) }, 'JSON' ) }) </script> </head> <body> <h1>Sample page</h1> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem. </p> <p>In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien. </p> <p>Donec ut est in lectus consequat consequat. Etiam eget dui. Aliquam erat volutpat. Sed at lorem in nunc porta tristique. Proin nec augue. Quisque aliquam tempor magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc ac magna. Maecenas odio dolor, vulputate vel, auctor ac, accumsan id, felis. Pellentesque cursus sagittis felis. </p> <p>Pellentesque porttitor, velit lacinia egestas auctor, diam eros tempus arcu, nec vulputate augue magna vel risus. Cras non magna vel ante adipiscing rhoncus. Vivamus a mi. Morbi neque. Aliquam erat volutpat. Integer ultrices lobortis eros. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin semper, ante vitae sollicitudin posuere, metus quam iaculis nibh, vitae scelerisque nunc massa eget pede. Sed velit urna, interdum vel, ultricies vel, faucibus at, quam. Donec elit est, consectetuer eget, consequat quis, tempus quis, wisi. </p> <p>In qui nunc. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Donec ullamcorper fringilla eros. Fusce in sapien eu purus dapibus commodo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras faucibus condimentum odio. Sed ac ligula. Aliquam at eros. Etiam at ligula et tellus ullamcorper ultrices. In fermentum, lorem non cursus porttitor, diam urna accumsan lacus, sed interdum wisi nibh nec nisl. </p> <div id='curloutput'> </div> </body> </html> 1 Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted December 3, 2018 Author Share Posted December 3, 2018 Thank you -- I think that will work perfect.... but made a mistake: the 3rd party data I am accessing is in XML format: <merchandise> <apiversion>4.0</apiversion> <items> <item> <name>Hammer</name> <price>7.50</price> <currency>USD</currency> </item> </items> </merchandise> I'm not sure how to modify the jQuery in your code to parse XML (the "$().ready(function()" portion). Simply changing "JSON" to "XML" or, var txt = resp["<price>"] doesn't work. I found some "solutions" on Google, but I would rather use concise style of scripting. How would I access, for example, the "price" in the XML? Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2018 Share Posted December 3, 2018 Process the XML on the server and send back the formattted text as you want it to display <?php /** * IF AJAX CALL, PROCESS */ if (isset($_GET['ajax'])) { $data = get_data('http://www.geoplugin.net/json.gp?ip=19.117.63.253'); // easist way is to use SimpleXML $xml = simplexml_load_string($data); // find the items $items = $xml->xpath("//item"); $output = "<pre>"; // format as you want it to display foreach ($items as $it) { $output .= sprintf("%-20s%8.2f %s\n", $it->name, $it->price, $it->currency); } $output .= "</pre>\n"; exit($output); } /** * curl call * * @param string $path cUrl url */ function get_data($path){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$path); curl_setopt($ch, CURLOPT_FAILONERROR,1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT, 15); $retValue = curl_exec($ch); curl_close($ch); // hard code retvalue for example purposes only $retValue = "<merchandise> <apiversion>4.0</apiversion> <items> <item> <name>Hammer</name> <price>7.50</price> <currency>USD</currency> </item> <item> <name>Screwdriver</name> <price>3.50</price> <currency>USD</currency> </item> <item> <name>Chisel</name> <price>5.50</price> <currency>USD</currency> </item> </items> </merchandise> "; return $retValue; } ?> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>Example ajax/curl</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type='text/javascript'> $().ready( function() { $.get( "", {"ajax" : 1 }, function(resp) { $("#curloutput").html(resp) }, 'TEXT' ) }) </script> </head> <body> <h1>Sample page</h1> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna. Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci. Aenean nec lorem. </p> <p>In porttitor. Donec laoreet nonummy augue. Suspendisse dui purus, scelerisque at, vulputate vitae, pretium mattis, nunc. Mauris eget neque at sem venenatis eleifend. Ut nonummy. Fusce aliquet pede non pede. Suspendisse dapibus lorem pellentesque magna. Integer nulla. Donec blandit feugiat ligula. Donec hendrerit, felis et imperdiet euismod, purus ipsum pretium metus, in lacinia nulla nisl eget sapien. </p> <p>Donec ut est in lectus consequat consequat. Etiam eget dui. Aliquam erat volutpat. Sed at lorem in nunc porta tristique. Proin nec augue. Quisque aliquam tempor magna. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nunc ac magna. Maecenas odio dolor, vulputate vel, auctor ac, accumsan id, felis. Pellentesque cursus sagittis felis. </p> <p>Pellentesque porttitor, velit lacinia egestas auctor, diam eros tempus arcu, nec vulputate augue magna vel risus. Cras non magna vel ante adipiscing rhoncus. Vivamus a mi. Morbi neque. Aliquam erat volutpat. Integer ultrices lobortis eros. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin semper, ante vitae sollicitudin posuere, metus quam iaculis nibh, vitae scelerisque nunc massa eget pede. Sed velit urna, interdum vel, ultricies vel, faucibus at, quam. Donec elit est, consectetuer eget, consequat quis, tempus quis, wisi. </p> <p>In qui nunc. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Donec ullamcorper fringilla eros. Fusce in sapien eu purus dapibus commodo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras faucibus condimentum odio. Sed ac ligula. Aliquam at eros. Etiam at ligula et tellus ullamcorper ultrices. In fermentum, lorem non cursus porttitor, diam urna accumsan lacus, sed interdum wisi nibh nec nisl. </p> <div id='curloutput'> </div> </body> </html> In this example the data is returned as Hammer 7.50 USD Screwdriver 3.50 USD Chisel 5.50 USD Quote Link to comment Share on other sites More sharing options...
StevenOliver Posted December 3, 2018 Author Share Posted December 3, 2018 Thank you!! That's perfect. That's the only thing I neglected to try -- modifying the code to process XML on the server. (Why why WHY don't I think of these things?! ?) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.