I have array, for example:
$links = array(
'http://aaa.com/data.txt',
'http://aaea.com/data.txt',
'http://aada.com/data.txt',
'http://agaa.com/data.txt',
'http://ahaa.com/data.txt',
'http://awha.com/data.txt',
'http://aaeha.com/data.txt',
//etc x100
);
And in PHP I am doing:
foreach ($links as $link) {
$data = file_get_contents($link);
//save data in database
}
It works fine, but terribly slow. How is better way for this with PHP? I would like get data asynchronous.
My other way - jQuery and Ajax queries from PHP script, but maybe exists better way?
I would suggest doing it this way.
<?php
$Links = array(
'http://aaa.com/data.txt',
'http://aaea.com/data.txt',
'http://aada.com/data.txt',
'http://agaa.com/data.txt',
'http://ahaa.com/data.txt',
'http://awha.com/data.txt',
'http://aaeha.com/data.txt'
);
$TempData = '';
foreach ($Links as $Link) {
$TempData .= file_get_contents($Link);
$TempData .= '|';
}
$Data = rtrim($TempData, '|');
// save the $Data string and when you export the
// string from the db use this code to turn it into an array
//
// $Data = explode('|' $ExportedData);
// var_dump($Data);
//
// If you do it this way you will be preforming 1 sql
// statement instead of multiple saving site resources
// and making you code execute faster
?>
If this helped let me know.
Related
I Have huge database and I have to compare same data with which I have stored in local storage as xml and i have to get it to the view something like below,
But belowfunction i get all data but can't view data because of loading time and can't laravel view render that much data at a time? Please help.
$files = File::allFiles(storage_path('xml'));
foreach($files as $filename1) {
$xml_file = file_get_contents($filename1, FILE_TEXT);
$xml = simplexml_load_string($xml_file, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$data[] = json_decode($json,TRUE);
}
//dd($data);
foreach($data as $val){
//dd($val);
foreach($val as $dan){
//dd($da);
foreach($dan as $da){
//dd($da);
$no[] = $da['JS_UniqueConsignRef'];
foreach($da['Order_Details'] as $n){
}
}
}
}
$shipment = Shipment::whereIn('JS_UniqueConsignRef',$no)->get()->chunk(1000);
foreach($shipment as $ship){}
//dd($ship);
return view('sitemap',compact('files','ship','shipment'));
}
You can use Queues to avoid this timeout issue - I highly advise you to look at the documentation for that.
One of the ways to implement is the following:
$xml_file = file_get_contents($filename1, FILE_TEXT);
dispatch(function () use ($xml_file) {
// Your logic here
});
If you need to pass back the information to the user, then you can use Job Events and a logic on your own to display it.
is there a way to get instagram page the contents of an post using simple php
i did some search and i found this script
$url = 'https://www.instagram.com/pagename/';
$str = file_get_contents($url);
$count = 0;
if(preg_match('#followed_by": {"count": (.*?)}#', $str, $match)) {
$count = $match[1]; // get the count from Regex pattern
}
echo $count;
but it is getting only number of follower is there a way
to get the contents of an Instagram post using same concept ?
Here's a code that works (today). But as #Andy said, it's not reliable and it's dirty af :)
<?php
$source = file_get_contents("https://www.instagram.com/p/POST_ID/");
preg_match('/<script type="text\/javascript">window\._sharedData =([^;]+);<\/script>/', $source, $matches);
if (!isset($matches[1]))
return false;
$r = json_decode($matches[1]);
print_r($r);
// Example to get the likes count
// $r->entry_data->PostPage[0]->graphql->shortcode_media->edge_media_preview_like->count
i took #Andy advice as he it's not reliable and it's dirty af
So this is what i found to go over the html
Instagram change their page markup, your application will break.
is this
$username = 'username';
$instaResult=
file_get_contents('https://www.instagram.com/'.$username.'/media/');
//decode json string into array
$data = json_decode($instaResult);
foreach ($data as $posts) {
foreach($posts as $post){
$postit = (array) json_decode(json_encode($post), True);
/* get post text and image */
echo '<p>' .$postit["caption"]["text"].'</p>';
echo '<img src="'.$postit["images"]["standard_resolution"]["url"].'" />';
echo "</br>-----------</br>";
}
}
I have a program that removes certain pages from a web; i want to then traverse the remaining pages and "unlink" any links to those removed pages. I'm using simplehtmldom. My function takes a source page ($source) and an array of pages ($skipList). It finds the links, and I'd like to then manipulate the dom to convert the element into the $link->innertext, but I don't know how. Any help?
function RemoveSpecificLinks($source, $skipList) {
// $source is the html source file;
// $skipList is an array of link destinations (hrefs) that we want unlinked
$docHtml = file_get_contents($source);
$htmlObj = str_get_html($docHtml);
$links = $htmlObj->find('a');
if (isset($links)) {
foreach ($links as $link) {
if (in_array($link->href, $skipList)) {
$link->href = ''; // Should convert to simple text element
}
}
}
$docHtml = $htmlObj->save();
$htmlObj->clear();
unset($htmlObj);
return($docHtml);
}
I have never used simplehtmldom, but this is what I think should solve your problem:
function RemoveSpecificLinks($source, $skipList) {
// $source is the HTML source file;
// $skipList is an array of link destinations (hrefs) that we want unlinked
$docHtml = file_get_contents($source);
$htmlObj = str_get_html($docHtml);
$links = $htmlObj->find('a');
if (isset($links)) {
foreach ($links as $link) {
if (in_array($link->href, $skipList)) {
$link->outertext = $link->plaintext; // THIS SHOULD WORK
// IF THIS DOES NOT WORK TRY:
// $link->outertext = $link->innertext;
}
}
}
$docHtml = $htmlObj->save();
$htmlObj->clear();
unset($htmlObj);
return($docHtml);
}
Please provide me some feedback as if this worked or not, also specifying which method worked, if any.
Update: Maybe you would prefer this:
$link->outertext = $link->href;
This way you get the link displayed, but not clickable.
I have a page test.php in which I have a list of names:
name1: 992345
name2: 332345
name3: 558645
name4: 434544
In another page test1.php?id=name2 and the result should be:
332345
I've tried this PHP code:
<?php
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTMLFile("/test.php");
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//*#".$_GET["id"]."");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
I need to be able to change the name with GET PHP method in test1.pdp?id=name4
The result should be different now.
434544
is there another way, becose mine won't work?
Here is another way to do it.
<?php
libxml_use_internal_errors(true);
/* file function reads your text file into an array. */
$doc = file("test.php");
$id = $_GET["id"];
/* Show your array. You can remove this part after you
* are sure your text file is read correct.*/
echo "Seeking id: $id<br>";
echo "Elements:<pre>";
print_r($doc);
echo "</pre>";
/* this part is searching for the get variable. */
if (!is_null($doc)) {
foreach ($doc as $line) {
if(strpos($line,$id) !== false){
$search = $id.": ";
$replace = '';
echo str_replace($search, $replace, $line);
}
}
} else {
echo "No elements.";
}
?>
There is a completely different way to do this, using PHP combined with JavaScript (not sure if that's what you're after and if it can work with your app, but I'm going to write it). You can change your test.php to read the GET parameter (it can be POST as well, you'll see), and according to that, output only the desired value, probably from the associative array you have hard-coded in there. The JavaScript approach will be different and it would involve making a single AJAX call instead of DOM traversing using PHP.
So, in short: AJAX call to test.php, which then output the desired value based on the GET or POST parameter.
jQuery AJAX here; native JS tutorial here.
Just let me know if this won't work for your app, and I'll delete my answer.
I'm requesting information from a remote server which is sent back to me as XML, I use SimpleXML to parse it. However I need to load multiple URLs, can I do so through one file, or do I need to have a different file for each request?
My code looks something like this
$url = 'http://...';
$xml = simplexml_load_file($url);
Thanks!
You can create a loop that deals with the multiple urls...
$all_urls = array('http://url1', 'http://url2', 'http://url3');
foreach ($all_urls as $url) {
$xml = simplexml_load_file($url);
}
Create a function and place '$xml = simplexml_load_file($url)' into it. Then you can call the function from within a LOOP. That's the only way I am able to get it to call simplexml_load_file($url) more than once within a loop.
$all_urls = array('url1', 'url2', 'url3');
foreach ($all_urls as $url) {
importXml($url);
}
function importXml($url){
$xml = simplexml_load_file($url);
//Do stuff...
}