Solved

cURL search

Posted on 2013-01-16
8
173 Views
Last Modified: 2013-01-16
Hi experts,

I need to use this for more fields with same name and don't know how to search for them.. Please help.

if (substr($str, strpos($str, 'some text')) !== false) {
$string=substr($str, strpos($str, 'fieldname')+10,20);
$string=substr($string, strpos($string, 'blah')+4,20);
$string=substr($string, strpos($string, '>')+1,100);
$string=substr($string, 0, strpos($string, '<'));
}

Thanks.
0
Comment
Question by:jebbie635
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38781780
Can you be little more clear on the scenario, how you are searching..You said curl in heading where is the curl code and how you are searching
0
 

Author Comment

by:jebbie635
ID: 38781822
I guess it's just php. The first part of my script is curl, sorry for that, am newbie. Please tell me how to search for the fields. This is what i want to do:

first search the source code of a page for 'some text', if found continue to search for labels named the same and then get text if label is found. This code is just for the first label which i already know it's there because it found 'some text'.

if (substr($str, strpos($str, 'some text')) !== false) {
$string=substr($str, strpos($str, 'labelname')+10,20);
$string=substr($string, strpos($string, 'blah')+4,20);
$string=substr($string, strpos($string, '>')+1,100);
$string=substr($string, 0, strpos($string, '<'));
}

How do i search for the next label which has the same name as first one? I need to do this for 20 labels.
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38782230
read the page and programactically find the list of labels in that page

Then a call a function with required parameters like label,serach term, etc..and return data from that function

Hope its clear
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38782320
Use the Simple HTML DOM library to parse the page - it's going to be far easier and more flexible. I wrote an article on how to use it to manipulate HTML pages, although you can use the same article to simply extract information from a page after you download it with cURL:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_10277-HTML-Manipulation-Made-Easy.html
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 500 total points
ID: 38782346
The basics are:

// Include the library
require("simple_html_dom.php");

// Assuming $str contains all the HTML from your cURL request
if (strpos($str, 'some text') !== false)
{
   // Parse the HTML
   $html = str_get_html($str);

   // Loop through all <blah> elements
   foreach($html->find("blah") as $blah)
   {
      // Show the contents of that blah tag
      echo "Outertext = " . $blah->outertext . "\n";
      echo "Innertext = " . $blah->innertext . "\n";
      echo "Plaintext = " . $blah->plaintext . "\n";
   }
}
0
 

Author Comment

by:jebbie635
ID: 38783771
$str=post($url, $referer, $agent, $cookie, $vars);

I need to search for this

<div class="someClass">someText</div>

because i have also other <div class="someClass"> with someOtherText which i don't need, and then get the link. I have to do this for 40 possible links.

$link=substr($str, strpos($str, 'someClass')+11,20);
$link=substr($link, strpos($link, 'someText')+14,100);
$link=substr($link, strpos($link, '<a href="')+9,1000);
$link=substr($link, 0, strpos($link, '>'));

Open in new window

This is what i have so far:

$link1=substr($str, strpos($str, 'someClass')+19,20);
$link1=substr($link1, strpos($link1, 'someText')+9,100);
$link1=substr($link1, strpos($link1, '<a href="')+9,1000);
$link1=substr($link1, 0, strpos($link1, '>'));

$link2=substr($str, strpos($str, 'someClass')+19,1000);
$link2=substr($link2, strpos($link2, 'someClass')+9,100);
$link2=substr($link2, strpos($link2, 'someText')+9,100);
$link2=substr($link2, strpos($link2, '<a href="')+9,1000);
$link2=substr($link2, 0, strpos($link2, '>'));
if ($link2) {
some action
}
...
$link40=substr($str, strpos($str, 'someClass')+19,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
...
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someClass')+9,40000);
$link40=substr($link40, strpos($link40, 'someText')+9,100);
$link40=substr($link40, strpos($link40, '<a href="')+9,1000);
$link40=substr($link40, 0, strpos($link40, '>'));
if ($link40) {
some action
}

Open in new window

Can you simplify the search of 'someClass'? This is a lot of code.
0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 38784792
Try my previous code, but replace this line:

foreach($html->find("blah") as $blah)

with:

foreach($html->find("div.someClass") as $div)

Then you can check the value of $div->plaintext and compare it to "someText" or whatever you're looking for.
0
 

Author Closing Comment

by:jebbie635
ID: 38784959
It works. Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now