PHP cURL same HTML element [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
I want to output the third element from the same HTML element. I know how to output a div or span with an id or a class but I have no idea how to output a same HTML element. I thought it was something with p[1] but it doesn't work.
I know there is a lot of answered questions about it but it never explained how to output the same HTML element without a class or id.
website : http://localhost/
<p>example</p>
<p>example1</p> <!-- i want to take this one -->
<p>example2</p>
-------------------
php script :
<?php $curl = curl_init('http://localhost/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$code3 = curl_exec($curl);
curl_close($curl);
$code = '/<p>(.*?)<\/p>/s';
$code6= preg_match($code, $code3, $code4);
echo $code4[1]
?>
/* doesn't work ..
also php.net doesnt give a good example about it so i hope someone can help me here.
thanks advanced !
*/

Try:
<?php
$curl = curl_init('http://justpaste.it/8s5v');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$content = curl_exec($curl);
curl_close($curl);
preg_match_all('/<p>(.+)<\/p>/im', $content, $result);
print_r($result);
print "Selected: " . $result[0][1] . "\n";
?>

Related

Passing a PHP Variable to curl_init [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
The following PHP code is working fine passing a specific IP Address to curl_init:
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=5.79.66.162');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$yummy = json_decode($data);
$country= $yummy->country; //this is working fine. country is being passed
===========================================
But I need to pass the ip address in a variable in the curl_init. However you cannot pass a PHP variable between single quotes. So the following curl_init statement is NOT working.
$ip="5.79.66.162";
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=$ip');
How does the code above = need to be changed in order for the call to work using a variable?
I would appreciate any help anyone can offer.
However you cannot pass a PHP variable between single quotes
Exactly, so you can use doble quotes here or string concatenation:
$ip="5.79.66.162";
$ch = curl_init("https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=$ip");
or
$ip="5.79.66.162";
$ch = curl_init('https://ipgeolocation.abstractapi.com/v1/?api_key=my_key&ip_address=' . $ip);

Scrape link in web page with specific class php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
as the title suggests, I would like to retrieve links that have a specific class.
I have the code to connect to the pages and with the preg_match function I would like to take only the url that is in href = "url".
the structure of the link I would like to take and the one found in href = "", this link is in a table and can also have other attributes but not id, only the view class.
<a title="viwe" class="view" href="link">blablabla</a>
while I wrote this code
$curl = curl_init('http://prove/prove/pag/test.php');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$page = curl_exec($curl);
if(curl_errno($curl)) // check for execution errors
{
echo 'Scraper error: ' . curl_error($curl);
exit;
}
curl_close($curl);
$regex = '/<a.*?>(.*?)<\/a>/';
if ( preg_match($regex, $page, $list) )
echo $list[0];
else
print "Not found";
Well, the method you are trying to implement is not recommended, yet if you have to, this expression might be closer to what you have in mind, that I'm guessing:
<a\s.*?\sclass="\s*view\s*"[^>]*>.*?<\/a>
Demo

My php gives "null" while it is something [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
So, I want to make my site check the bitcoin prices. I do that with virwox. So, I send a curl to http://api.virwox.com/api/json.php?method=getBestPrices&symbols[0]=BTC/SLL.
That gives me this:
{"result":[{"symbol":"BTC\/SLL","errorCode":"OK","bestBuyPrice":"983021","bestSellPrice":"1009989"}],"error":null,"id":""}
Now, I want the bestBuyPrice. So, I runned a json_decode on my $output. Then my $output is an array. So, I did json_encode("$output["result"]") to see what will come out of that. It gave me this:
[{"symbol":"BTC\/SLL","errorCode":"OK","bestBuyPrice":"983021","bestSellPrice":"1009989"}]
So, I tought if I do json_decode("$output["bestBuyPrice"]") I would get it, but I didn't. What I got was: null. How can I fix this?
Here is my full code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.virwox.com/api/json.php?method=getBestPrices&symbols[0]=BTC/SLL");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$decode = json_decode($output, true);
$encode = json_encode($decode["result"]["bestBuyPrice"], true);
echo $encode; // Gives null
$koop = $decode["result"]["bestBuyPrice"]; // No result
$verkoop = $decode["result"]["bestSellPrice"]; // No result
echo $koop . "<br />".$verkoop;
?>
Thanks!
This one's real easy. Get rid of the quotes from around your variable!
json_encode("$output["result"]")
to
json_encode($output["result"])
EDIT:
Based on the var_dump in the comment below, it looks like there is another array key you need:
$koop = $decode["result"][0]["bestBuyPrice"];
Give that a try!

Get the table details from a URL [duplicate]

This question already has answers here:
PHP Web scraping of Javascript generated contents [duplicate]
(2 answers)
Closed 7 years ago.
I just want to get the table details from the HTML and for example the URL is,
$url="https://www.centralbank.org.bz/rates-statistics/exchange-rates";
From this,I need to get the currency rate table in this url and also remove all the dirty data.
Please help me,
Many thanks
Try this code ::
$url = 'https://www.centralbank.org.bz/rates-statistics/exchange-rates';
$content = file_get_contents($url);
$first_step = explode( '<table id="currencyTable">' , $content );
$second_step = explode("</table>" , $first_step[1] );
echo $second_step[0];
You should use Simple HTML DOM,
An example may be helpful to you:
<?php
include('simple_html_dom.php');
$url = 'https://www.phpbb.com/community/viewtopic.php?f=46&t=543171';
$html = file_get_html($url);
$links = array();
foreach($html->find('a[class="postlink"]') as $a) {
$links[] = $a->href;
}
print_r($links);
?>

preg_match_all matches unexpectedly [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I've just started PHP and I want to scrape a little page which I can't, I tried doing 'PREG_MATCH_ALL' but it just doesn't get the result I want.. Basically I want to scrape the youtube video links from here only: https://gdata.youtube.com/feeds/api/standardfeeds/most_shared - Scrape all of them and then use them later.
I tried using the following code which failed;
<?php
$data = file_get_contents('https://gdata.youtube.com/feeds/api/standardfeeds/most_shared');
preg_match_all("/src='(.+?)'>/", $data, $links);
$link_out = $links[0][0];
echo $link_out;
?>
I'm new to PHP, so little help please.
Thanks
As the feed is XML, you can use PHP's SimpleXMLElement to obtain the data.
<?php
$xml = new SimpleXMLElement(
'https://gdata.youtube.com/feeds/api/standardfeeds/most_shared',
null,
true
);
foreach($xml->entry as $entry) {
echo $entry->content['src'], PHP_EOL;
}
/*
https://www.youtube.com/v/IjWc43FCYlg?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/Xw1C5T-fH2Y?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/Kq0_dGKx4Os?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/gbcBYs0ljI0?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/78juOpTM3tE?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/OOiZ-5DqwYI?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/zjz614QVyfQ?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/h15m87WsCHQ?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/SXKOTdyOUBg?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/BRAM8MpqIeA?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/5yB3n9fu-rM?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/NAOo9SnzRH8?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/0KtILkzC-1g?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/kWSIFh8ICaA?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/Mi6AhogZCeg?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/kWuIGAZ1x2I?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/lKY5fmDGVLs?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/C94PaCtqOk4?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/V-fL8zopddI?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/UWlzMIl7E48?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/mcw6j-QWGMo?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/-RSDaRttpzk?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/8_RDx4skTp4?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/7YDWdv9kR0M?version=3&f=standard&app=youtube_gdata
https://www.youtube.com/v/m96tYpEk1Ao?version=3&f=standard&app=youtube_gdata
*/
Anthony.
Try with this pregmatch:
preg_match_all("/src='([^']+)'/si", $data, $links);
and show results:
echo "<pre>";
print_r($links);
<?php
$data = file_get_contents('https://gdata.youtube.com/feeds/api/standardfeeds/most_shared');
preg_match_all("/src='(.+?)'\/>/", $data, $links);
print_r($links[1]);
You forgot to match the closing / of the anchor tags.

Categories