Stop the running function completely when click on Stop button - php

I have list of URLs to be scraped. I get these URLs from a html table and in a controller function I put them in a foreach loop. I have two buttons Start scraping and Stop scraping.
I am running with a scraping function which is written in controller and called by an ajax function on a button click of Start scraping button.
Now my requirement is to stop the current ongoing process when I click on Stop scraping button.
I have googled a lot and tried use of Gloabal variables , sessions etc etc but all this works only after the completion of first process that comes in pending state un network.
Please help me that how can I stop the current php process when I click on Stop scraping button.
Below is the ajax call function for Start scraping
$("#get_data").click(function()
{
var values = new Array();
$.each($("input[name='case[]']:checked"), function()
{
var data = $(this).parents('tr:eq(0)');
values.push({
'weburl':$(data).find('td:eq(3)').text() ,
'id' : $(data).find('td:eq(2)').text() ,
'state':$(data).find('td:eq(6)').text()
});
});
sendmyarray(values);
});
function sendmyarray(values)
{
var location = '<?php echo base_url()?>';
var x = $.ajax({
type: "POST",
url: location+'admin/AdminController/getArrayAndScrap',
data: {myData:JSON.stringify(values)},
beforeSend: function() {
$("#stop_get_data").css("display", "block");
$("#get_data").prop('disabled', true); // disable button
},
success: function (response) {
$("#get_data").prop('disabled', false);
$("#stop_get_data").css("display", "none");
console.log(response);
if(response == 'stop')
{
alert("Process Stopped");
}
if(response == " ends")
{
alert("Process completed. Please check the status of the URLs processed!");
}
}
});
}
// BElow is the controller function //
//-- Function to get the array of selected row's url and state ; and scraping them accordingly
public function getArrayAndScrape()
{
if(isset($_POST['myData']))
{
$json = $_POST['myData'];
$myDataArray = json_decode($json,true);
foreach ($myDataArray as $url)
{
$status = $this->CommonModel->getSingleData('dtl_scrap_status','ws_detail',array('dtl_scrap_id' => $url['id']));
if( $status['dtl_scrap_status'] != 'Scrapped')
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 12); // 12 seconds
curl_setopt($ch, CURLOPT_URL, $url['weburl']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($ch);
if($html === false)
{
$this->CommonModel->addData('ws_detail' , array('dtl_scrap_status' => curl_error($ch) , 'dtl_scrap_id' => $url['id'] , 'dtl_state' => $url['state'] , 'dtl_website' => $url['weburl']));
$this->CommonModel->updateData('ws_site_scrap_detail' , array('site_last_scrap_date' => date('Y-m-d')) , array('site_id' => $url['id']));
}
else
{
$doc = new DOMDocument();
#$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title'); // TITLE
$metas = $doc->getElementsByTagName('meta'); // Meta
$head = $doc->getElementsByTagName('h1'); // H1
if(isset($nodes->item(0)->nodeValue))
{
$title = $nodes->item(0)->nodeValue;
}
if(isset($head->item(0)->nodeValue))
{
$heading = $head->item(0)->nodeValue;
}
if(isset($metas))
{
for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'keywords') // Keyword
$keywords = $meta->getAttribute('content');
if($meta->getAttribute('name') != '')
{
if($meta->getAttribute('name') == 'description') // meta description
{
$description = $meta->getAttribute('content');
}
}elseif($meta->getAttribute('property') != '')
{
if($meta->getAttribute('property') == 'og:description') // meta Og:description
{
$OgDescription = $meta->getAttribute('content');
}
}
}
}
curl_close($ch);
if(isset($url['state']))
{
$addDtl['dtl_state'] = $url['state'];
}
if(isset($heading))
{
$addDtl['dtl_program_name'] = $heading;
}
if(isset($title))
{
$addDtl['dtl_program_provider'] = $title;
}
if(isset($description))
{
$addDtl['dtl_program_description'] = $description;
}elseif(isset($OgDescription))
{
$addDtl['dtl_program_description'] = $OgDescription;
}
if(isset($keywords))
{
$addDtl['dtl_program_keywords'] = $keywords;
}
if(isset($url['weburl']))
{
$addDtl['dtl_website'] = $url['weburl'];
}
if(isset($url['id']))
{
$addDtl['dtl_scrap_id'] = $url['id'];
}
$addDtl['dtl_scrap_status'] = 'Scrapped';
if($this->CommonModel->addData('ws_detail' , $addDtl))
{
$this->CommonModel->updateData('ws_site_scrap_detail' , array('site_last_scrap_date' => date('Y-m-d')) , array('site_id' => $url['id']));
}else
{
// echo "Error";exit();
}
} // End of else
} // End of status check
else
{
$this->CommonModel->updateData('ws_site_scrap_detail' , array('site_last_scrap_date' => date('Y-m-d')) , array('site_id' => $url['id']));
// echo "alscrapped";exit();
}
} // End of foreach
echo "ends";
} // End of if(isset)
}
// Something I want below scenario (AJAX CALL)//
$('#stop_get_data').click(function()
{
return false (from the function which is in running state after click event of start scraping);
});
Any help would be important for me.
Thanks

It's not possible to terminate a php script with javascript. The PHP script will continue running its loop until it finishes. PHP executes on the server side while javascript is executed on the browser. There may be a way to do something like this with sockets but I really don't know enough about them to give you any advice on how to go about it.
What you could do is run the loops on the javascript side and make individual calls to the php function for each iteration. This would allow you to stop the process with javascript whenever you like.

Related

jquery $.post function is not executing the function

I am trying to post data to populate my HTML dropdowns using jQuery's $.post as shown below.
var county = $("#county");
var constituency = $("#constituency");
var ward = $("#ward");
populate_fields();
$('select[name="political"]').change(function(ev) {
ev.preventDefault();
populate_fields();
});
function populate_fields() {
// call the server side script, and on completion, update all dropdown lists with the received values.
county.html('').append($('<option>').text('Please choose a county'));
constituency.html('').append($('<option>').text('Please choose a constituency'));
ward.html('').append($('<option>').text('Please choose a ward'));
var data = {
// pass all the currently selected values to the server side script.
"county" : county.val(),
"constituency" : constituency.val(),
"ward" : ward.val()
}
$.post('php/dropdown.php', data, function (resp) {
console.log('function works');
all_values = resp;
$.each(all_values.county, function () {
$option = $("<option>").text(this).val(this);
if (all_values.current_county == this) {
$option.attr('selected', 'selected');
}
county.append($option);
});
$.each(all_values.constituency, function () {
$option = $("<option>").text(this).val(this);
if (all_values.current_constituency == this) {
$option.attr('selected', 'selected');
}
constituency.append($option);
});
$.each(all_values.ward, function () {
$option = $("<option>").text(this).val(this);
if (all_values.current_ward == this) {
$option.attr('selected', 'selected');
}
ward.append($option);
});
},'json');
}
the php file being referred to works perfectly, however the post does not receive data from this file and the function does not execute, I have traced the problem to the above snippet of code. Anyone have an Idea as to what could be the problem?
Here is my php Code as requested for more insight into the problem:
<?php
// read the CSV file in the $makes_models_years array
$makes_models_years = array();
//$uploads_folder = wp_upload_dir()['basedir'];
$file = fopen("./resources/polling_data.csv","r");
$firstline = true;
while (($line = fgetcsv($file)) !== FALSE) {
if ($firstline) {
$firstline = false;
continue;
}
$makes_models_years[$line[0]][$line[1]][] = $line[2];
}
fclose($file);
// setup the initial array that will be returned to the the client side script as a JSON object.
$return_array = array(
'county' => array_keys($makes_models_years),
'constituency' => array(),
'ward' => array(),
'current_county' => false,
'current_constituency' => false
);
// print_r($return_array);
// collect the posted values from the submitted form
$make = key_exists('county', $_POST) ? $_POST['county'] : false;
$model = key_exists('constituency', $_POST) ? $_POST['constituency'] : false;
$year = key_exists('ward', $_POST) ? $_POST['ward'] : false;
// populate the $return_array with the necessary values
if ($make) {
$return_array['current_county'] = $make;
$return_array['constituency'] = array_keys($makes_models_years[$make]);
if ($model) {
$return_array['current_constituency'] = $model;
$return_array['ward'] = $makes_models_years[$make][$model];
if ($year) {
$return_array['current_ward'] = $year;
}
}
}
// encode the $return_array as a JSON object and echo it
echo json_encode($return_array);
print_r($return_array);
die();
?>
I realised that the issue was in the post not being able to read data returned by the php file. I solved it by placing the php file in the same directory as the javascript file, apparently they don't work well when in placed separate folders.

php : link preview or url preview not working

Using ubuntu server, 18.04, php 7.2. php.ini includes allow_url_fopen= on. Disabling the firewall didn't work. I also tried searching for 6 hours now applied some random answers didn't work.
This is my code so far:
if (preg_match('#(?<=^|(?<=[^a-zA-Z0-9-_\.//]))((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.\,]*(\?\S+)?)?)*)#', htmlspecialchars_decode(stripslashes($urlData)), $results)) {
$page = $results[0];
$page = $this->addScheme($page);
// $default_socket_timeout = ini_get('default_socket_timeout');
// ini_set('default_socket_timeout', 3);
$content = file_get_contents("https://api.urlmeta.org/?url=".$page);
// ini_set('default_socket_timeout', $default_socket_timeout);
if ($content) {
$data = json_decode($content, true);
$urlResult = $data['result'];
$urlResponse = $data['meta'];
if ($urlResult['status'] === "OK") {
$urlLink = $page;
if (isset($urlResponse['image'])) {
$urlImage = $urlResponse['image'];
}
if (isset($urlResponse['description'])) {
$urlDescription = $urlResponse['description'];
}
if (isset($urlResponse['title'])) {
$urlTitle = $urlResponse['title'];
}
}
}
}

Reload or Refresh Page after comment successfully submitted in Ajax

I am trying to reload the page after the comment is successfully submitted
Comment submitted through Ajax
After this line
// Save the comment
$state = $comment->store();
If i am trying to use location.reload(); as
// Save the comment
$state = $comment->store();
location.reload();
then its not working and comment not even submitting
Below is complete code
Can any one help - how to refresh and load page when comment is successfully submitted and stored in backend
<?php
defined('_JEXEC') or die('Unauthorized Access');
require_once(JPATH_COMPONENT . '/views/views.php');
class EasyBlogViewComments extends EasyBlogView
{
/**
* Processes comment saving
*
* #since 4.0
* #access public
* #param string
* #return
*/
public function save()
{
// Check for request forgeries
EB::checkToken();
// Test if user is really allowed to post comments
if (!$this->acl->get('allow_comment')) {
return $ajax->reject(JText::_('COM_EASYBLOG_NO_PERMISSION_TO_POST_COMMENT'));
}
// Default values
$moderated = false;
$parentId = $this->input->get('parentId', 0, 'int');
$depth = $this->input->get('depth', 0, 'int');
$subscribe = $this->input->get('subscribe', false, 'bool');
$email = $this->input->get('email', '', 'email');
$message = $this->input->get('comment', '', 'default');
$name = $this->input->get('name', '', 'default');
$username = $this->input->get('username', '', 'default');
$password = $this->input->get('password', '', 'default');
$title = $this->input->get('title', '', 'default');
$terms = $this->input->get('terms', false, 'bool');
$blogId = $this->input->get('blogId', 0, 'int');
$isCB = $this->input->get('iscb', 0, 'int');
// If there is no name, and the current user is logged in, use their name instead
if (!$name && $this->my->id) {
$user = EB::user($this->my->id);
$name = $user->getName();
}
// Validate the email
$data = array('post_id' => $blogId, 'comment' => $message, 'title' => $title, 'email' => $email, 'name' => $name, 'username' => $username, 'terms' => $terms);
// Load up comment table
$comment = EB::table('Comment');
$state = $comment->validatePost($data);
if (!$state) {
return $this->ajax->reject($comment->getError());
}
// Bind the data on the comment table now
$comment->bindPost($data);
// Check for spams
if ($comment->isSpam()) {
return $this->ajax->reject(JText::_('COM_EASYBLOG_SPAM_DETECTED_IN_COMMENT'));
}
$captchaResponse = EB::captcha()->verify();
// Perform captcha verification
if (isset($captchaResponse->success) && $captchaResponse->success == false) {
return $this->ajax->reject($captchaResponse->errorCodes);
}
// Get current date
$date = EB::date();
// Set other attributes for the comment
$comment->created = $date->toSql();
$comment->modified = $date->toSql();
$comment->published = true;
$comment->parent_id = $parentId;
$comment->created_by = $this->my->id;
// Process user registrations via comment
$register = $this->input->get('register', '', 'bool');
if ($register && $this->my->guest) {
if (empty($password) || empty($username) || empty($email)) {
return $this->ajax->reject('COM_EASYBLOG_COMMENT_REGISTRATION_FIELD_EMPTY');
}
$userModel = EB::model('Users');
$id = $userModel->createUser($username, $email, $name, $password);
if (!is_numeric($id)) {
return $this->ajax->reject($id);
}
$comment->created_by = $id;
}
$totalComments = $this->input->get('totalComment', 0, 'int');
// Determines if comment moderation is enabled
if ($this->config->get('comment_moderatecomment') == 1 || ($this->my->guest && $this->config->get('comment_moderateguestcomment'))) {
$comment->published = EBLOG_COMMENT_STATUS_MODERATED;
}
// Load up the blog table
$blog = EB::table('Blog');
$blog->load($comment->post_id);
// If moderation for author is disabled, ensure that the comment is also published automatically.
if ((!$this->config->get('comment_moderateauthorcomment') && $blog->created_by == $this->my->id) || EB::isSiteAdmin()) {
$comment->published = true;
}
// Update the ordering of the comment before storing
$comment->updateOrdering();
// Save the comment
$state = $comment->store();
if (!$state) {
return $this->ajax->reject($comment->getError());
}
$resultMessage = JText::_('COM_EASYBLOG_COMMENTS_POSTED_SUCCESS');
$resultState = 'success';
// If user registered as well, display a proper message
if ($register) {
$resultMessage = JText::_('COM_EASYBLOG_COMMENTS_SUCCESS_AND_REGISTERED');
}
if ($comment->isModerated()) {
$resultMessage = JText::_('COM_EASYBLOG_COMMENT_POSTED_UNDER_MODERATION');
$resultState = 'info';
}
// Process comment subscription
if ($subscribe && $this->config->get('main_subscription') && $blog->subscription) {
$subscribeModel = EB::model('Subscription');
$subscribeModel->subscribe('blog', $blog->id, $email, $name, $this->my->id);
}
// Process comment notifications
$comment->processEmails($comment->isModerated(), $blog);
// Set the comment depth
$comment->depth = $this->input->get('depth', 0, 'int');
// Update the sent flag
$comment->updateSent();
// Format the comments
$result = EB::comment()->format(array($comment));
$comment = $result[0];
$language = JFactory::getLanguage();
$rtl = $language->isRTL();
$theme = EB::template();
$theme->set('comment', $comment);
$theme->set('rtl', $rtl);
$output = '';
if ($isCB) {
// if the is saving from CB plugin, then we need to display the output using different template.
$output = $theme->output('site/comments/cb.item');
} else {
$output = $theme->output('site/comments/default.item');
}
return $this->ajax->resolve($output, $resultMessage, $resultState);
}
public function reloadCaptcha()
{
$ajax = EB::ajax();
// Get the previous captcha id.
$id = $this->input->get('previousId', 0, 'int');
$captcha = EB::table('Captcha');
$state = $captcha->load($id);
if ($state) {
$captcha->delete();
}
// Generate a new captcha
$captcha = EB::table('Captcha');
$captcha->created = EB::date()->toSql();
$captcha->store();
$image = EB::_('index.php?option=com_easyblog&task=captcha.generate&tmpl=component&no_html=1&id=' . $captcha->id, false);
return $ajax->resolve($image, $captcha->id);
}
2nd File
EasyBlog.module('comments/form', function($) {
var module = this;
EasyBlog.require()
.script('comments/captcha', 'comments/list')
.library('markitup')
.done(function($) {
EasyBlog.Controller('Comments.Form', {
defaultOptions: {
"{formWrapper}": "[data-comment-form-wrapper]",
"{form}": "[data-comment-form]",
"{title}": "[data-comment-title]",
"{name}": "[data-comment-name]",
"{username}": "[data-comment-username]",
"{password}": "[data-comment-password]",
"{email}": "[data-comment-email]",
"{register}": "[data-comment-register]",
"{website}": "[data-comment-website]",
"{counter}": "[data-comment-counter]",
"{subscribe}" : "[data-comment-subscribe]",
"{terms}": "[data-comment-terms]",
"{tncLink}": "[data-comment-tnc]",
"{parentId}" : "[data-comment-parent-id]",
"{commentDepth}": "[data-comment-depth]",
"{blogId}" : "[data-comment-blog-id]",
"{depth}": "[data-comment-depth]",
"{notice}": "[data-comment-notice]",
"{editor}": "[data-comment-editor]",
"{submit}": "[data-comment-submit]",
"{formToken}": "[data-comment-token]",
"{recaptcha}": "[data-recaptcha-item]"
}
}, function(self, opts, base) {
return {
init: function() {
self.initEditor();
self.list = self.addPlugin('list');
// If recaptcha is enabled, we should skip the normal captcha
var recaptcha = self.recaptcha.inside(self.element).length;
if (recaptcha < 1) {
self.captcha = self.addPlugin('captcha');
}
},
initEditor: function() {
if (self.editor().data('comment-bbcode') == 1) {
self.editor().markItUp(window.EasyBlogBBCodeSettings);
}
},
setNotice: function(message, type) {
var className = '';
if (type == 'error') {
className = 'alert-danger';
}
if (type == 'success') {
className = 'alert-success';
}
if (type == 'info') {
className = 'alert-info';
}
self.notice()
.removeClass('hide')
.addClass('alert ' + className)
.html(message);
},
resetForm: function() {
// If the comment form has a parent id, we need to reposition the comment form back.
var parentId = self.parentId().val();
if (parentId != 0) {
self.form().appendTo(self.formWrapper());
}
// Reset the form
self.username().val('');
self.password().val('');
self.subscribe().attr('checked', false);
self.editor().val('');
self.website().val('');
self.name().val('');
self.depth().val(0);
self.parentId().val(0);
self.trigger('resetForm');
// Determine if recaptcha is available
var recaptcha = self.recaptcha.inside(self.element);
// Get recaptcha's response
if (recaptcha.length > 0) {
grecaptcha.reset();
}
},
resetNotice: function() {
self.notice()
.removeClass('info error')
.html('');
},
"{self} replyComment": function(el, event, commentItem, commentId, commentDepth) {
// Hide notices in the reply form
self.notice().addClass('hide');
// When user tries to reply to an existing comment, move the form next to the level of the comment item
commentItem.after(self.form());
self.depth().val(commentDepth);
// Set the new parent id to the comment's id
self.parentId().val(commentId);
},
"{self} cancelReply": function(el, event, commentItem, commentId) {
// Set the parent id to 0
self.parentId().val(0);
// Reset the comment depth back to 0
self.depth().val(0);
// Relocate the form back to it's origin
self.formWrapper().html(self.form());
},
"{self} commentAdded": function()
{
// Increment the counter
var count = self.counter().html();
count = parseInt(count) + 1;
self.counter().html(count.toString());
self.resetForm();
},
getValues: function() {
var data = {
title: self.title().val(),
name: self.name().val(),
email: self.email().val(),
username: self.username().val(),
password: self.password().val(),
website: self.website().val(),
subscribe: self.subscribe().is(':checked') ? 1 : 0,
register: self.register().is(':checked') ? 1 : 0,
comment: self.editor().val(),
terms: self.terms().is(':checked') ? 1 : 0,
depth: self.depth().val(),
parentId: self.parentId().val(),
blogId: self.blogId().val()
};
// token
// data[self.formToken().attr('name')] = 1;
// Determine if recaptcha is available
var recaptcha = self.recaptcha.inside(self.element);
// Get recaptcha's response
if (recaptcha.length > 0) {
data.recaptcha = grecaptcha.getResponse();
}
self.trigger('submitComment', [data]);
return data;
},
"{tncLink} click": function() {
EasyBlog.dialog({
content: EasyBlog.ajax('site/views/comments/terms')
})
},
"{submit} click" : function(el, event) {
event.preventDefault();
// Reset notices
self.resetNotice();
// Add loading indicator on the button
$(el).attr('disabled', true);
var tmp = $(el).html();
$(el).html('<i class="fa fa-repeat fa-spin"></i>');
// Get the form values
var data = self.getValues();
// Perform an ajax call to submit the comment
EasyBlog.ajax('site/views/comments/save', data)
.done(function(output, message, state) {
self.setNotice(message, state);
self.trigger('commentAdded',[output, data]);
})
.fail(function(message) {
self.setNotice(message, 'error');
})
.always(function(){
$(el).removeAttr('disabled');
$(el).html(tmp);
self.trigger('reloadCaptcha');
});
return false;
}
}
});
module.resolve();
});
});
I would hazard a guess that the place to add the location.reload() would be within this snippet:
"{self} commentAdded": function()
{
// Increment the counter
var count = self.counter().html();
count = parseInt(count) + 1;
self.counter().html(count.toString());
self.resetForm();
},
So perhaps like:
"{self} commentAdded": function()
{
// Increment the counter
var count = self.counter().html();
count = parseInt(count) + 1;
self.counter().html(count.toString());
self.resetForm();
/* reload */
location.reload();
},
Or, you could add it to the .done method within EasyBlog.ajax .... pretty much the same thing I think by looking at the code though

long polling ajax is not keeping realtime updates

i was doing long polling , but it can see in my network chrome my long polling in status pending that means is waiting for new data and changes the database , but when I try to insert one item to my database then , but when I try to insert one value show keep that long polling in real time i cant see real time updatings .. how can I fix it?
pusher controller codeigiter
public function pusher() {
header('Content-Type: application/json');
$user_id = $this->session->log['id'];
set_time_limit(0);
while (true) {
$firstCall = false;
if (isset($_GET['timestamp'])) {
$last_ajax_call = $_GET['timestamp'];
} else {
$last_ajax_call = time();
$firstCall = true;
}
clearstatcache();
$notificationsCount = $this->notification->checkForNotifications($last_ajax_call, $user_id);
$newData = (int) $notificationsCount > 0 ? true : false;
$notifications = [];
if ($newData) {
$dataSet = $this->notification->getNotifications($user_id, $last_ajax_call);
foreach($dataSet as $data) {
$notifications[] = $data;
$finalNotificationTime = $data['timestamp'];
}
$result = array('notifications' => $notifications, 'timestamp' => $finalNotificationTime);
$json = json_encode($result);
echo $json;
break;
} else {
if ($firstCall) {
$dataSet = $this->notification->getUnreadNotifications($user_id);
foreach($dataSet as $data) {
$notifications[] = $data;
}
$result = array('notifications' => $notifications, 'timestamp' => $last_ajax_call);
$json = json_encode($result);
echo $json;
break;
}
sleep(1);
session_write_close();
continue;
}
}
exit();
}
ajax
function check_notifications(timestamp) {
var queryString = {
'timestamp': timestamp
};
$.ajax({
type: 'GET',
url: URL_GET_NOTIFICATION,
data: queryString,
success: function (data) {
// put result data into "obj"
for (var i in data.notifications) {
notify(data.notifications[i].message, data.notifications[i].type, data.notifications[i].timestamp);
}
check_notifications(data.timestamp);
}
});
}
check_notifications();

PHP Scrape - Create multidimensional arrays from results - current code only returning one result

I'm quite new to PHP and am creating a web scraper for a project. From this website, https://www.bloglovin.com/en/blogs/1/2/all, I am scraping the blog title, blog url, image url and concatenating a follow through link for later use. As you can see on the page, there are several fields with information for each blogger.
Here is my PHP code so far;
<?php
// Function to make GET request using cURL
function curlGet($url) {
$ch = curl_init(); // Initialising cURL session
// Setting cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch); // Executing cURL session
curl_close($ch); // Closing cURL session
return $results; // Return the results
}
$blogStats = array();
function returnXPathObject($item) {
$xmlPageDom = new DomDocument();
#$xmlPageDom->loadHTML($item);
$xmlPageXPath = new DOMXPath($xmlPageDom);
return $xmlPageXPath;
}
$blPage = curlGet('https://www.bloglovin.com/en/blogs/1/2/all');
$blPageXpath = returnXPathObject($blPage);
$title = $blPageXpath->query('//*[#id="content"]//div/a/h2/span[1]');
if ($title->length > 0) {
$blogStats['title'] = $title->item(0)->nodeValue;
}
$url = $blPageXpath->query('//*[#id="content"]//div/a/h2/span[2]');
if ($url->length > 0) {
$blogStats['url'] = $url->item(0)->nodeValue;
}
$img = $blPageXpath->query('//*[#id="content"]//div/a/div/#href');
if ($img->length > 0) {
$blogStats['img'] = $img->item(0)->nodeValue;
}
$followLink = $blPageXpath->query('//*[#id="content"]/div[1]/div/a/#href');
if ($followLink->length > 0) {
$blogStats['followLink'] = 'http://www.bloglovin.com' . $followLink->item($i)->nodeValue;
}
print_r($blogStats);
/*$data = $blogStats;
header('Content-Type: application/json');
echo json_encode($data);*/
?>
Currently, this only returns:
Array ( [title] => Fashion Toast [url] => fashiontoast.com [followLink] => http://www.bloglovin.com/blog/4735/fashion-toast )
My question is, what is the best way to loop through each of the results? I've been looking through Stack Overflow and am struggling to find an answer to my question, and my heads going a bit loopy! If anyone could advise me or put me in the right direction, that would be fantastic.
Thank you.
Update:
I'm very sure this is wrong, i'm receiving errors!
<?php
// Function to make GET request using cURL
function curlGet($url) {
$ch = curl_init(); // Initialising cURL session
// Setting cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch); // Executing cURL session
curl_close($ch); // Closing cURL session
return $results; // Return the results
}
$blogStats = array();
function returnXPathObject($item) {
$xmlPageDom = new DomDocument();
#$xmlPageDom->loadHTML($item);
$xmlPageXPath = new DOMXPath($xmlPageDom);
return $xmlPageXPath;
}
$blogPage = curlGet('https://www.bloglovin.com/en/blogs/1/2/all');
$blogPageXpath = returnXPathObject($blogPage);
$blogger = $blogPageXpath->query('//*[#id="content"]/div/#data-blog-id');
if ($blogger->length > 0) {
$blogStats[] = $blogger->item(0)->nodeValue;
}
foreach($blogger as $id) {
$blPage = curlGet('https://www.bloglovin.com/en/blogs/1/2/all');
$blPageXpath = returnXPathObject($blPage);
$title = $blPageXpath->query('//*[#id="content"]//div/a/h2/span[1]');
if ($title->length > 0) {
$blogStats[$id]['title'] = $title->item(0)->nodeValue;
}
$url = $blPageXpath->query('//*[#id="content"]//div/a/h2/span[2]');
if ($url->length > 0) {
$blogStats[$id]['url'] = $url->item(0)->nodeValue;
}
$img = $blPageXpath->query('//*[#id="content"]//div/a/div/#href');
if ($img->length > 0) {
$blogStats[$id]['img'] = $img->item(0)->nodeValue;
}
$followLink = $blPageXpath->query('//*[#id="content"]/div[1]/div/a/#href');
if ($followLink->length > 0) {
$blogStats[$id]['followLink'] = 'http://www.bloglovin.com' . $followLink->item($i)->nodeValue;
}
}
print_r($blogStats);
/*$data = $blogStats;
header('Content-Type: application/json');
echo json_encode($data);*/ ?>
maybe you want to actually add a dimension to your array. I guess bloggers have a unique id, or somesuch identifier.
moreover, your code seems to execute only once? it might need to be in something like a foreach
I can't do that part for you, but you need an array containing each blogger, or a way to do a while, or for! you have to understand how to iterate over your different bloggers by yourself :)
here an exemple of array of bloggers
[14]['bloggerOne']
[15]['bloggerTwo']
[16]['bloggerThree']
foreach ($blogger as $id => $name)
{
$blPage = curlGet('https://www.bloglovin.com/en/blogs/1/2/' . $name);
// here you have something to do so that $blPage is actually different with each iteration, like changing the url
$blPageXpath = returnXPathObject($blPage);
$title = $blPageXpath->query('//*[#id="content"]//div/a/h2/span[1]');
if ($title->length > 0) {
$blogStats[$id]['title'] = $title->item(0)->nodeValue;
}
$url = $blPageXpath->query('//*[#id="content"]//div/a/h2/span[2]');
if ($url->length > 0) {
$blogStats[$id]['url'] = $url->item(0)->nodeValue;
}
$img = $blPageXpath->query('//*[#id="content"]//div/a/div/#href');
if ($img->length > 0) {
$blogStats[$id]['img'] = $img->item(0)->nodeValue;
}
$followLink = $blPageXpath->query('//*[#id="content"]/div[1]/div/a/#href');
if ($followLink->length > 0) {
$blogStats[$id]['followLink'] = 'http://www.bloglovin.com' . $followLink->item($i)->nodeValue;
}
}
so after the foreach, you array could look like:
['12345']['title'] = whatever
['url'] = url
['img'] = foo
['followLink'] = bar
['4141']['title'] = other
['url'] = urlss
['img'] = foo
['followLink'] = bar
['7415']['title'] = still
['url'] = url4
['img'] = foo
['followLink'] = bar

Categories