Page 1 of 1
Take a value from another site, php help
Posted: Tue Aug 16, 2011 9:10 am
by niki_noki
Hello all
I have a one question. How can I get value from another site or I send a POST to another site?
Now I have this code
(html)
<form action="" method="get">
<input type="text" value="">
<select>
<option>site1</option>
<option>site2</option>
<option>site3</option>
<option >site4</option>
<option >site5</option>
</select>
<input type="submit" value="Search" />
</form>
And I choose "site1" go to "site1" and then send POST on this site(search form).
I've heard that happens to with cURL
I hope you understand what I, , , and sorry for my english but don't speak very good
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 9:24 am
by Celauran
Getting a value from another site and sending a POST request to another site are two different things, but both can be accomplished through
cURL.
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 9:42 am
by niki_noki
Celauran wrote:Getting a value from another site and sending a POST request to another site are two different things, but both can be accomplished through
cURL.
The first one I want send POST request and get result on the search.
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 9:56 am
by phphelpme
Code: Select all
$address = "www.domain.com/index.php";
$postdata = "your data here for the form fields"; // Example: ?search=XXXX&value=YYYY
$search = curl_init($address);
curl_setopt($search, CURLOPT_URL, $address);
curl_setopt ($search, CURLOPT_POST, 1);
curl_setopt ($search, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($search, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($search, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($search);
I hope this helps you get started but without more detail about sites you are refering too and more details regards the data you want to extract then I can only guess. This is a basic markup to help you get started.
I suggest you look at:
http://php.net/manual/en/book.curl.php
Best wishes
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 10:06 am
by niki_noki
Code: Select all
$address = "www.potursi.eu/search.php";
$postdata = "?search=68"; // Example: ?search=XXXX&value=YYYY
$search = curl_init($address);
curl_setopt($search, CURLOPT_URL, $address);
curl_setopt ($search, CURLOPT_POST, 1);
curl_setopt ($search, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($search, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($search, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($search);
echo "$search";
With this code show this:
Resource id #1
Any ideas ?! Why don't get a more result
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 10:13 am
by phphelpme
Code: Select all
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://www.potursi.eu/search.php?search=".$value);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
//echo $output;
Now you have told us the site you wish to search this should work and load the contents into a string value for you to extract whatever you wish.
You will need a form to stipulate what the value of $value will be (which would be the search term).
On your attempt you tried to echo the wrong string value, you needed to echo $result not the $search.
Best wishes
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 10:26 am
by niki_noki
@phphelpme , thank you but I have one more question.
How can I give a some <div> on this page?
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 10:28 am
by phphelpme
I dont understand what you mean by give it some div?
I think the question here is do you need to extract info from the page to display on yours or do you just want to load the search results in another window but have the initial search form on your site?
Best wishes
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 10:36 am
by niki_noki
With the cURL we give any information (search) but give the all page. I want a give only any <div> on page?
Sorry but I don't speak good english
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 10:40 am
by phphelpme
It sounds to me like your saying you want to show sections of the results on your own webpage?
If that is so then you need to now research substr() and strpos() functions to extract data from your $output value. Just echoing your $output string is just going to load the whole page that has been stored in that variable.
You need to decide what information you want to locate and extract on your page, then use strpos() to locate your chosen content and use substr() to extract that value.
You can check out this thread also:
viewtopic.php?f=1&t=131086 to get more advice and a link when it comes to your html tags.
Best wishes
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 11:18 am
by niki_noki
phphelpme wrote:It sounds to me like your saying you want to show sections of the results on your own webpage?
If that is so then you need to now research substr() and strpos() functions to extract data from your $output value. Just echoing your $output string is just going to load the whole page that has been stored in that variable.
You need to decide what information you want to locate and extract on your page, then use strpos() to locate your chosen content and use substr() to extract that value.
You can check out this thread also:
viewtopic.php?f=1&t=131086 to get more advice and a link when it comes to your html tags.
Best wishes
Is there any option to extract a certain div without substr ()
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 11:31 am
by phphelpme
You can extract a certain div from the $output by locating it first with strpos() and then finding the end of that div with strpos() then using substr() to extract that div only. It is not an easy task running through the code of the search page to find your particular div and it would work best if you had some type of unique value to it which meant you could search directly for it in the $output variable.
Just as an example of substr() and strpos() usage:
Code: Select all
$findme = '<div class="unique">';
$pos = strpos($output, $findme) + strlen($findme);
$id_start = substr($output, $pos);
$id_strip = strpos($id_start, "</div>", 1);
$id_end = substr($id_start, 0, $id_strip);
echo $id_end;
Its not a simple task to do unfortunately and you really need to research the use of these functions but to answer your question I am not aware of another common way to extract only a certain <div>.
You will run into complications when you have no unique <div> it you come accross more than one so you have to code which div it is you want to extract otherwise the code will stop at the first div if finds. You will also have to deal with the potential HTML Tags embedded within your content because if you copy these with the content you will then display this on your site which you might not want.
I suggest looking into that previous post I linked for you just incase you need to strip the tags from the content or at least ignore them from the count.
Best wishes
Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 12:08 pm
by niki_noki
Thank you !

Re: Take a value from another site, php help
Posted: Tue Aug 16, 2011 12:25 pm
by phphelpme
Your very welcome
Best wishes