Wwriting links to database in a foreach loop fails - php

I am using simple html dom to write records to a database, however it doesn't seem to write the records.
The problem is with the foreach loop. It outputs all the urls and the following error:
Notice: Undefined variable: url in C:\xampp\htdocs\meh\crawler.php on line 28
<?php
// Create connection
$con=mysqli_connect("localhost","root","spidermankillssuperman","expatriates");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
include_once('../simple_html_dom.php');
$links = array (
'http://www.expatriates.com/classifieds/bhr/hs/index100.html'
);
foreach ($links as $link) {
$html = file_get_html($link);
foreach($html->find('a') as $element) {
if(strpos($element->href, "cls"))
$url = "http://expatriates.com".$element->href . '<br>';
echo $url;
}
$sql="INSERT INTO urlstocrawl (url)
VALUES ('$url')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo '<br>'.'<p>'."1 record added";
}
mysqli_close($con);
?>

you want your mysql query to insert foreach link in you html file, not run once after the loop. move the code inside the loop:
foreach ( $html->find('a') as $element ) {
if ( strpos($element->href, "cls") !== false ) {
$url = "http://expatriates.com" . $element->href . '<br>';
$sql = "INSERT INTO urlstocrawl (url) VALUES ('$url')";
if ( !mysqli_query($con,$sql) ) {
die('Error: ' . mysqli_error($con));
}
echo '<br>'.'<p>'."1 record added" . $url;
}
}
Note the modification to the if condition. strpos() returns either false when the string is absent or an integer indicating the position, starting at 0. This means the condition could fail if cls is at the start of the string. With this strict type checking you can be sure of the desired behavior.

Related

php simple html dom return array?

I'm trying to create an app using the simple HTML DOM php script, but im running into a wall currently with it - hope you can help.
The aim is to read product numbers from mySQL database, concat these numbers with a url constant and then parse this resulting website for a specific class. The result should then be printed to the screen.
My problem is that the script is returning only the first result from the function array, i have tried a few things, such as trying to call $prices->children[4], but nothing seems to help.
When i trigger the function get_prices_array() with a url, it brings back multiple results -> but when i return this inside my while loop it only brings back the first result in the array.
Heres my code, hope you can point me in the right direction!
Thanks!
<?php
include('simple_html_dom.php');
function get_prices_array($url) {
$x = file_get_html($url);
foreach ($x->find('span.price')as $dom) {
$y = $dom->outertext;
return $y;
}
$x->clear();
}
$con = mysqli_connect("localhost", "***", "***", "***");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$result = mysqli_query($con, "SELECT * FROM articles");
while ($row = mysqli_fetch_array($result)) {
$constant = '***';
$prices = get_prices_array($constant . $row["product_number"]);
echo $row["product_number"] . " - " . $prices . '<br />';
}
mysqli_close($con);
?>
// EDIT //
I changed the function get_prices_array() to loop through each span price class and add the result to an array, the array is then returned from the function. The first 5 results from the array are then stored to variables and added to the return string. Thanks for your help!
<?php
include('simple_html_dom.php');
function get_prices_array($url) {
$x = file_get_html($url);
$y = array();
foreach ($x->find('span.price')as $dom) {
$x = ($dom->outertext);
$y[] = $x;
}
return $y;
//$x->clear();
}
$con = mysqli_connect("localhost", "***", "***", "***");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$result = mysqli_query($con, "SELECT * FROM articles");
while ($row = mysqli_fetch_array($result)) {
$constant = '***';
$prices = get_prices_array($constant . $row["product_number"]);
$pos1 = $prices[0];
$pos2 = $prices[1];
$pos3 = $prices[2];
$pos4 = $prices[3];
$pos5 = $prices[4];
echo $row["product_number"] . " - " . $pos1 . " - " . $pos2 . " - " . $pos3 . " - " . $pos4 . " - " . $pos5 .'<br />';
}
mysqli_close($con);
?>
I think problem is because of return used in foreach loop of function get_prices_array($url).
it is executing foreach loop only once. There is no meaning of loop if you are returning without condition inside loop.

How to save data to a database from file_get_html($url)?

I can't save data to database from file_get_html($url) function. My script is showing data nicely by scraping from a url. But I can't save the showed data to a database. It shows error between object and array. I can't even show data by array index. Such as $value[0]. Here is my code sample:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
}
mysqli_close($con);
?>
Try this and let me know if it works:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
}
$value = base64_encode($value);
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
mysqli_close($con);
?>
$value need convert to a string before write to DB. You can show array or object use
echo '<pre>';
print_r($value);
echo '</pre>';
or
var_dump($value)

Insert Data into Mysql from PHP from a nested foreach loop

I'm trying to enter data fetched from a json into a database. I have fetched the data as a multidimensional array and used a foreach loop to loop through the arrays.
I'm trying to insert this data into a mysql database but I keep getting an error Error: Field 'summary' doesn't have a default value.
My database table is called articles in a database called tracking and has the following fields: article_id, url, domain, favicon, title, summary, likes, tweets, plusones, image, category
my php file is called json_parser.php and it is as follows
<?php
define('DB_NAME', 'tracking');
define('DB_USER', 'xxxxxxxxxxx');
define('DB_PASSWORD', 'xxxxxxxxx');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link){
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if(!$db_selected){
die('Cannot use '. DB_NAME . ': ' .mysql_error());
}
$url = "http://digitalrand.net/api/url_data/?key=abacus&pass=aba123cuxza%";
$json = file_get_contents($url);
$obj = json_decode($json, true);
foreach($obj as $article_array){
$url = $article_array['url'];
$domain = $article_array['domain'];
$favicon = $article_array['favicon'];
$title = $article_array['title'];
$category = $article_array['category'];
echo $category . "<br>";
echo $domain . "<br>";
echo $favicon . "<br>";
echo $title . "<br>";
echo $url . "<br>";
$sql = 'INSERT INTO articles '.
'(url, domain, favicon, title) '.
'VALUES ( "$url", "$domain","$favicon","$title" )';
if (mysql_query($sql)){
echo "success.......";
}
if(!mysql_query($sql)){
die('Error: ' . mysql_error());
}
$large_summary = $article_array['summary'];
foreach ($large_summary as $summary){
mysql_query("INSERT INTO articles(summary) VALUES('$summary')");
echo "$summary <br>";
}
$images = $article_array['images'];
foreach ($images as $image){
$image_first= reset($image);
echo $image_first;
mysql_query("INSERT INTO articles(image) VALUES($image_first)");
echo "<img src=$image_first>";
}
$social_shares = $article_array['social_shares'];
foreach($social_shares as $social_share=>$include){
echo $social_share . ": " . $include . "<br>";
}
$entities = $article_array["entities"];
foreach($entities as $entity_cat=>$entities_arr){
foreach($entities_arr as $key=>$entity){
echo $entity_cat.' >> '.$entity. "<br>";
}
}
}
?>
How do I loop through the array items and display them on appropriate fields
First of all, you have an error in your code :
foreach ($large_summary as $summary){
mysql_query("INSERT INTO articles(summary) VALUES($summary)");
echo "$summary ";
}
foreach ($large_summary as $summary){
mysql_query("INSERT INTO articles(summary) VALUES('$summary')");
echo "$summary ";
}
Then, keep in mind that you are INSERTING row with only (url, domain, favicon, title) fields, and then some rows with only (summary) and then other one with only (image).
I think you wan't to insert into the same row no?
Then if you wan't to make it works, you have to set a default value :
ALTER TABLE `articles` CHANGE `summary` `summary` TEXT NULL DEFAULT NULL;
[EDIT to answer to the Mutuma comment]
Your database isn't set correctly. You have few sumaries for one group of (url, domain, favicon, title).
So you have to create another table, like summaries with (id, ref_article, summary)...
And it hte same for images!
Please reconsider the base structure.

Grabbing data from steamapi (json) and importing it to mysql

I can't quite figure out how to import data from remote json (steamapi) into MySql.
<?php
$con = mysql_connect("localhost","XXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXX");
mysql_select_db('XXXXXXXXXXXXXXXXX',$con);
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXX&steamids=76561198033811393";
$json = file_get_contents($url);
$result = json_decode($json);
foreach($result as $key => $value)
{
if($value)
{
mysql_query("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate)
VALUES ($value->steamid,
$value->communityvisibilitystate,
$value->profilestate)");
}
mysql_close($con);
}
?>
Steam JSON Image(click for full size):
You can read each player using $data->response->players, it will be assigned to $player where then, you can directly access their object like $player->steamid. See below example:
$data = json_decode($json);
foreach($data->response->players as $player)
{
echo $player->steamid, "\n\n";
}
I also suggest you change your query like this:
$query = sprintf("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate) VALUES ('%s','%s','%s')",
mysql_real_escape_string($player->steamid),
mysql_real_escape_string($player->communityvisibilitystate),
mysql_real_escape_string($player->profilestate));
To prevent any further issues like broken strings or injections.
Resulting code for the SQL part would be:
// Build the query
$query = sprintf("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate) VALUES ('%s','%s','%s')",
mysql_real_escape_string($player->steamid),
mysql_real_escape_string($player->communityvisibilitystate),
mysql_real_escape_string($player->profilestate));
// Perform Query
$insert = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$insert)
{
echo 'Invalid query: ' . mysql_error() . "\n";
echo 'Whole query: ' . $query . "\n";
echo "------------------\n";
}
Given your actual code it would look like this:
<?php
$con = mysql_connect("localhost","XXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXX");
mysql_select_db('XXXXXXXXXXXXXXXXX',$con);
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXX&steamids=76561198033811393";
$json = file_get_contents($url);
$data = json_decode($json);
foreach($data->response->players as $player)
{
// Build the query
$query = sprintf("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate) VALUES ('%s','%s','%s')",
mysql_real_escape_string($player->steamid),
mysql_real_escape_string($player->communityvisibilitystate),
mysql_real_escape_string($player->profilestate));
// Perform Query
$insert = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$insert)
{
echo 'Invalid query: ' . mysql_error() . "\n";
echo 'Whole query: ' . $query . "\n";
echo "------------------\n";
}
}
mysql_close($con);
Use this
<?php
$id = $_GET['id'];
$key = 'xxx';
$link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=76561198033811393&format=json');
$myarray = json_decode($link, true);
$result = $myarray['response']['players'][0];
echo $result->steamid;
?>
if you get more then one result use foreach format given below
$result = $myarray['response']['players'];
foreach($result as $key => $value)
{
echo $value->steamid."<br>";
}

printing out a json array

I am trying to figure out how to print a json array. I am trying to handle this from Android code but it is not working. I believe the problem lies in how I am outputting json. the below doesn't show anything. The script is below:
<?php
// Create connection
$conn=mysqli_connect("localhost","dhdkahd","dsdajdsa","dsadjsajd");
$json = array();
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (!$conn->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $conn->error);
}
$sql='SELECT title, description, country, city, rate FROM discounts';
$rs=$conn->query($sql);
if($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
/*$rs->data_seek(0);
while($row = $rs->fetch_assoc()){
echo $row['title'] . '<br>';
}*/
while ( $row = $rs->fetch_assoc() )
{
$json[] = json_encode($row,JSON_UNESCAPED_UNICODE);
}
}
//echo json_decode($json);
echo json_encode($json);
mysqli_close($conn);
?>
thanks in advance
You can only call json_encode once. You're double-encoding everything.
The line where you're adding data to the array needs to be
$json[] = $row;
Then, when the array is built up, you encode the entire thing in one single call:
echo json_encode($json);
You call json_encode twice. To fix it, change your code to:
if($rs === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
/*$rs->data_seek(0);
while($row = $rs->fetch_assoc()){
echo $row['title'] . '<br>';
}*/
while ( $row = $rs->fetch_assoc() )
{
$json[] = $row;
}
}
//echo json_decode($json);
echo json_encode($json);
mysqli_close($conn);
?>

Categories