Curl Problem, I just don't get it - php

SOLVED: var_dump revealed that that the issue was of & and & .
Also I would still like to know why curl fetched a page with missing divs. Would anyone be kind enough to explain ?
have been at this problem for 1.5hours now. Narrowed down to something that just doesn't make sense to me. Please help . I am fetching a page using curl.
The problem is that when the following code is used:
a lot of div tags from the navigation to the left are missing from the fetched page,
$pageToParse = "http://www.themarketgrocer.com.au{$arrayLeftBar[$i]} " ;
;
//fetch target page
$curlFetched = http_get($pageToParse, "http://www.google.com") ; //fetched using a curl function
echo $curlFetched['FILE'];
the value of $arrayLeftBar[$i] is fetched from an html page using simplehtmldom. as echoed:
"/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=92"
HOWEVER if
$pageToParse = "http://www.themarketgrocer.com.au{$arrayLeftBar[$i]} " ;
is changed to : (basically manually appending the value of $arrayLeftBar[$i] )
$pageToParse = "http://www.themarketgrocer.com.au/index.php?option=com_content&view=category&layout=blog&id=37&Itemid=92" ;
then the complete page is fetched.
WHY ?? I have echoed the values and they are identical. What silly little thing am I missing?
Edit:
THis is how the $arrayLeftBar is being populated: I am pretty sure the problem is here . Coz if I manually create a simple array. Things work.
foreach ($sublevelLinks as $link)
{
$arrayLeftBar[] = $link->href ;
}

On the $arrayLeftBar[$i] value use urldecode
$query = urldecode($arrayLeftBar[$i]);
$pageToParse = "http://www.themarketgrocer.com.au{$query}" ;

SOLVED: var_dump revealed that that the issue was of & and & .
html_entity_decode($arrayLeftBar[$i]) did the necessary conversion

Related

PHP ECHO to pass two Var's via URL

echo "<li><a href=\"#\" onclick='load_page_device(\"mg-device.php?device=device_name&sensor=$p1_url\")'>".$row['p1_name']."</a></li>";
Alright I simply trying to pass two variables in a url via a php echo statement. It's simply not working.
I am then using
$device_name = $_GET['device'];
$sensor_name = $_GET['sensor'];
I have tried also to use & instead of & only. The first variable always seems to get passed but the second one never does. Any help is greatly appreciated I have been trying to get this to work for hours now. Thanks!
try
echo '<li>'.$row['p1_name'].'</li>';
you need to first check that $_GET['device'] , $_GET['sensor'] is set
try
if(isset($_GET['device']) && isset($_GET['sensor'])){
//stuff
}else{
//other stuff
}

extracting links as clickable links from sql table

Been at this for a while and cant figure it out so i thought id come to the ppl who know.
my php / sql is so-so but.....this is what im trying to do / figure out..
i have a database setup to take in messages (of any sort) and works fine. The user enters data in a field, hits send, and the message gets logged.
But when a user puts in a link for example " http://www.google.com "
its stored in the DB just fine but when its printed back onto the page, it comes back as plain
text, what i want is that when the page throws back the message with the link in it, that the link is live and clickable.
Ive googled my a$# off but im guessing im searching for the wrong thing (or im missing something unbeknownst to me. )
Any tips/*direction* etc, ill gladly accept. also, I dont mind doing the work/research so if you have links only ill take those too.
Thanks in advanced.
You need to parse messages plain text in order to find links. Then, change link texts by adding an anchor tag (<a>).
This link should help you.
you need regular expressions to detect links and convert them to ...
see the answer here:
PHP validation/regex for URL
<?php echo "your_field"; ?>
Something like this should work (I took this from some code I wrote for a client):
$query = mysql_query("SELECT * FROM wcordersinfo WHERE dealer_id = '" . $dealer_id . "' && drawing_num = " . $_GET['reference_id'] . "");
while ($orders = mysql_fetch_array($query)) {
if ($orders['drawing_num'] != '') {
$link_open = '<a href="http://www.example.com/dealers/order-details.php?reference_id=' . $orders['drawing_num'] . '">';
$link_close = '</a>';
} else {
$link_open = $link_close = '';
}
and then where you want to display the content:
<?php echo "<li>' . $link_start . $orders['carrier'] . $link_end . '</li>"; ?>
You may want to check out http://www.php.net/manual/en/function.preg-replace.php for replacing regular expressions with something.
There you want to replace every website with a typical html link syntax like ...

Possible to use PEAR SearchReplace to replace text within a .php file?

I came across this simple pear tutorial over here: http://www.codediesel.com/php/search-replace-in-files-using-php/
include 'File/SearchReplace.php' ;
$files_to_search = array("fruits.txt") ;
$search_string = "apples";
$replace_string = "oranges";
$snr = new File_SearchReplace($search_string,
$replace_string,
$files_to_search,
'', // directorie(s) to search
false) ;
$snr->doSearch();
echo "The number of replaces done : " . $snr->getNumOccurences();
The writer uses the fruits.txt file as an example.
I would like to do a search and replace on a .php file.
Basically what I am trying to achieve would be this:
On a user interaction, index.php is opened,
$promoChange = "%VARYINGTEXT%";
is searched for and replaced with
$promoChange = "$currentYear/$currentPromotion";
The $current variables will vary, hence the need to change the words inbetween the "" only.
Does anyone have any input on how this type of task could be accomplished?
If anyone knows of any tutorials relating to this subject, that too would be greatly appreciated.
Thank you!
I do have everything else figured out, regarding the template and user interaction, I am just having trouble trying to work out how to accomplish this type of search and replace. I have an understand of how it should be done as I have made something similiar using visual basic. But I am starting to this that my answer for this would be perl? I hope that this is not so...
Okay, my problem is partly solved with this:
// Define result of Activate click
if (isset($_POST['action']) and $_POST['action'] == 'Activate')
{
include ''.$docRoot.'/includes/pear/SearchReplace.php' ;
$files = array( "$docRoot/promotions/index.php" ) ;
$snr = new File_SearchReplace( '$promoChange = "";', '$promoChange = "'.$currentYear.'/'.$currentPromotion.'";', $files) ;
$snr -> doSearch() ;
}
but how do i get it to search and replace something like $promoChange = "%VARYINGTEXT%";
It found and replaced "" with the current session values. But now that is has changed, I need it to replace and text inbetween "AND".
Any ideas anyone?
If you only need to adapt a single file, then do it manually:
$src = file_get_contents($fn = "script.php");
$src = str_replace('"%VARYINGTEXT%"', '"$currentYear/$currentPromotion"', $src);
file_put_contents($fn, $src);
str_replace is sufficient for your case.
Why on earth do you want to do something like that? Frameworks like PHP do exist solely on the base of not having to write a page for each different view of the same interaction. What's wrong with just including the PHP page you now want to change, and set the variables accordingly before calling it?
Ontopic: I don't see why what you're doing is a problem, purely technically speaking. This can be done using PHP. But really, you shouldn't.

Weird issue with Simple HTML DOM and extracting the data through a nested loop

I apologize if this is a newbie question, but I cannot figure out why this doesn't work - and I can't seem to find anything about it when searching.
Basically, I am trying to scrape some userdetails from our site, that are not available from the sites REST api, so I have to do it manually. I have compiled a textfile with userids, that I use for fetching the wanted details from each user through Simple HTML Dom.
<?php
include('simple_html_dom.php') ;
include('functions.php') ;
$file = fopen("userids2.txt", "r") ;
while(!feof($file)) {
$userid = fgetss($file) ;
$url = 'http://<our URL>/user/'.$userid ;
echo $url ;
webscraper($url) ;
}
fclose($file) ;
?>
and here are the contents of functions.php:
<?php
function webscraper($loopurl) {
$html = new simple_html_dom();
$html->load_file($loopurl);
$test = $html->getElementsById('ctl00_ContentPlaceHolderDefault_UserViewUC_tabContainer_tabProfile_userProfile_ddWork') ;
foreach ($test as $element) {
echo $element ;
}
}
?>
The specific textfile used contains 4 userids that I know contain the information that I want. When I run the script it will only give me the output from the url from the last line in the textfile. It prints out the URLs fine, but refuses to load the remote html for the first three entries. If I delete the last line of the textfile, it then loads the new last line (which it refused to do before).
Any ideas?? Thanks in advance.
Doh.. I found out what the problem was. There was an "invisible" end of line character on all entries in the textfile EXCEPT the last one. So that was why it refused to work. Adding trim when retrieving the line fixed the problem:
$userid = trim(fgetss($file));
I probably should have known this, but at least I won't make this mistake next time :-).

delete a row from my sql table

I'm still new to php and working my way around it but i'm stuck at the following piece:
code for deleting a row in my table
i have a link directing towards this piece of my script. i run through the first half just fine but when i press on submit and try to execute my delete query it won't go to my second if statement let alone get to the delete query.
$pgd is the page id
my hunch is there is problem with the action in the form i'm building after my while statement
forgive me for the wierd formatting of my msg but its 2am and very tired, i promise to format my questions in the future better! any help is appreciated
edit: ok other then the obvious mistake of missing method=post #.#;
edit:
hey everyone,
first of all, i'd like to thank everyone for their response.
i just started coding in php last weekend so forgive my messy codes. the code is still running locally and my main goal was to finish the functions and then work on securing my code.
now back to the issue, i'm sorry if i was vague about my problem. i'll try to reiterate it.
my issue isn´t selecting an item i want to delete, the issue is that it won´t get to the 2nd if statement.
Re-edit:
this time with my current code:
if($_GET['delete'] == "y")
{
//content hier verwijderen
$sqlcont1="SELECT * FROM content where id ='".$_GET['id']."'";
echo $sqlcont1;
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while($rowcont1= mysql_fetch_array($resultcont1)){
echo '<form class="niceforms" action="?pg='.$pgd.'&delete=y&remove=y&id='.$_GET['id'].'" method="post">';
echo '<h1>'.$rowcont1['Titel'].'</h1>';
echo '<p>'.$rowcont1['Content'].'</p>';
echo '<input type="submit" value="Delete article">';
echo '</form>';
}
if($_GET['remove']=="y"){
echo 'rararara';
$id=$_GET['id'];
$sqlrem="DELETE FROM content WHERE id="$id;
echo $sqlrem;
mysql_query($sqlrem);
}
}
echoing $sqlrem gives me the following now:
DELETE FROM content WHERE id=8
that being my current code, i get in to the second IF statement but now to get it to delete!
#everyone:
ok maybe thinking out loud or following my steps worked but the code works, i know its very messy and it needs fine tuning. i'd like to thank everyone for their help and feedback. i'm liking this and you'll probably see me alot more often with nubby questions and messy codes with no escapes :(
First of all, you have SQL injection vulnerability in your script. Anyone can add some string that will be attached to your query, possibly altering it in a way that can make almost anything with the data from your database.
Escape your values with one of anti-SQL-injection methods. Read more for example on php.net/manual/en/function.mysql-query.php
To the point...
Your deletion code will be executed only if you invoke URL with two params (remove and delete set to y. That means your URL should look similar to something.php?delete=y&remove=y. Maybe you just did not spot it.
Please give details about any errors that occured and tell me whether the above mentioned solution helped.
mysql_fetch_array() returns an array
your while statement acts as an if, and does not iterate thru the array returned as you think it does
you need something like
$all_rows = mysql_fetch_array($result);
foreach ($all_rows as $row) {
$sql = "delete from table where id = " . $row['id'];
}
It looks to me like you're mixing two forms together here: you're wanting to see if you went to the delete row form (the first few lines), and you're trying to present the delete row form (the while loop.) I would break these two things apart. Have a page that simply displays your forms for row deletes, and another page that processes those requests. And another page that brings you to the delete rows page.
For now, just echo all the values you're expecting to receive in $_GET[] and see if they are what you expect them to be.
You have a lot of problems in that script alone, so just to make things easier (considering you uploaded a pic), put an
echo $sqlrem;
in your second if statement, see if the query is displayed. If not, it means it doesn't even get to that part of code, if it gets displayed, copy it and run it in phpmyadmin. That should output a more coherent error message. Tell us what that is and we'll work it through.
I also noticed that your DELETE SQL query might have an issue. If your $pgd' id is a integer, you shouldn't include the ' single quote, that is for string only.
**Correction**
$sqlrem = "DELETE FROM content WHERE id = " . controw1['id'];
EDIT
Anyway, just to help out everyone, I typed out his code for easier viewing.
I think his error is $rowcont1['Tilel'] --> that might caused PHP to have an error because that column doesn't exist. I assumed, it should be `Title' causing an typo error.
if(_$GET['delete'] == "y") {
$sqlcont1 = "SELECT * FROM content where id ='" . $_GET['id'] . "'";
$resultcont1 = mysql_query($sqlcont1) or die (include 'oops.php');
while ($rowcont1 = mysql_fetch_array($resultcont1)) {
echo '<form class = "niceforms" action = "?pg=' .$pgd . '&delete=y&remove=y">';
echo '<h1>' . $rowcont1['Title'] . '<h1>'; // <-- error here
echo '<p>' . $rowcont1['Content'] . '</p>';
echo '<input type = "submit" value = "Delete article">';
echo '</form>';
}
if ($_GET['remove'] == "y"){
$sqlrem = "DELETE FROM content WHERE id = " . $rowcont1['id'];
mysql_query ($sqlrem);
}
}

Categories