So I have this function which I'm trying to make with cURL because of server securities file_get_contents() is disabled.
$url ='https://example.com' .
'/b/'.urlencode($this->user_ID).'/o/'.urlencode($this->ID);
$url=$url.'/h/'.urlencode($hash);
$number=rand(0, 10)/10 ."";
$url=$url.$number;
$html = file_get_contents($url);
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];
What I have now is this
$url ='https://example.com' .
'/b/'.urlencode($this->user_ID).'/o/'.urlencode($this->ID);
$number=rand(0, 10)/10 ."";
$url=$url.$number;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$matches = curl_exec($ch);
curl_close($ch);
$html = file_get_contents($url);
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];
Is it something like this or I'm totally on wrong way? And how to change now this line - $html = file_get_contents($url); ?
You must remove the call to file_get_contents after the curl, you already have the response from the curl request.
replace this
$matches = curl_exec($ch);
with this
$html = curl_exec($ch);
Then do preg_match on the response
// $html = file_get_contents($url); this line is not needed
preg_match('/<td valign="bottom" class="sum">(.*?)<\/td\>/', $html , $matches);
return $matches[0];
Finally, inspect the contents of $matches and $matches[0]
Related
Using PHP CURL i'm calling one URL and i'm getting some response from one page,Now I need to replace one string from that response but it's not working,please check my code below.
$url = "My URL";
$url1 = $url1 = str_replace(' ', '%20', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$raw_data1 = curl_exec($ch);
curl_close($ch);
$raw = str_replace('#', 'Test', $raw_data1);
echo $raw;
You are trying to replace special character,My suggestion is you should try to use preg_replace() instead of str_replace()
Try the below example:
$url = "My URL";
$url1 = $url1 = str_replace(' ', '%20', $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$raw_data1 = curl_exec($ch);
curl_close($ch);
$raw = preg_replace("([#]+)", "Test", $raw_data1);
echo $raw;
To replace a pattern of string always use preg_replace() and in your case the response is coming from other source so better way to replace the string is to find define a pattern and replace Using preg_replace()
This is what you need preg_replace("([#]+)", "test", $raw_data1);
Could someone look at the script below, why do not list the contents of wonder ( < div > < / div > ) , even if the address is correct .
<?php
function get_content($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
$divs = '/<div>(.+)<\/div>/U';
preg_match($divs, $string, $matches);
$vysledek = $matches[1];
var_dump($vysledek);
var_dump($url);
}
echo get_content ("http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html");
?>
var_dump me writes :
NULL
string ( 59 ) " http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html "
Earlier I used :
function ziskatlyrics($url)
{
$content = file_get_contents($url);
$first_step = explode( '<div>' , $content );
$second_step = explode("</div>" , $first_step[1] );
$obsah = strip_tags($second_step[0]);
return $obsah;
}
echo ziskatlyrics("http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html");
But it throws me this error:
Warning : file_get_contents (
http://www.azlyrics.com/...ightsun.html
) : Failed to open stream: HTTP request failed ! in
I can do something to make it functional again ?
Thank you for answer.
Because you're just var_dump()'ing variables, not the response from the curl request...
var_dump($vysledek);
var_dump($url);
What you want is to assign the curl response to a varaible and return that...
$response = curl_exec($ch);
return $response;
Well...after you've done the regex you're doing in there, as I assume your $string is empty.
I do not know if I'm so correctly:
function get_content($url)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = curl_exec ($ch);
ob_end_clean();
$divs = '/<div>(.+)<\/div>/U';
preg_match($divs, $string, $matches);
$vysledek = $matches[1];
return $vysledek;
}
echo get_content ('http://www.azlyrics.com/lyrics/garthbrooks/midnightsun.html');
And the result: link
When i get the $url varible by preg_replace feedback it doesn't work but it i specify the $url value with the commented line it works. What's wrong in my code please? Many thanks.
$content = preg_replace('/(plugin_[^ ]+)/', getPlugin('$1'), $content);
function getPlugin($plugin) {
$url = "http://".$_SERVER['HTTP_HOST']."/{$plugin}.php?language=en";
//$url = "http://".$_SERVER['HTTP_HOST']."/plugin_contact.php?language=en";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
My solution
if (preg_match("/(plugin_[^ ]+)/", "$content", $match)) {
$content = preg_replace('/(plugin_[^ ]+)/', getPlugin($match[0]), $content);
}
Your original code doesn't work because getPlugin() runs before preg_replace(). It is called with the literal '$1' as argument, there is nobody to replace $1 with something else because preg_replace() is not involved.
I suppose you are refactoring a call to preg_replace() that uses the deprecated e(PREG_REPLACE_EVAL) modifier:
$content = preg_replace('/(plugin_[^ ]+)/e', 'getPlugin("$1")', $content);
In order to remove the /e modifier you can use preg_replace_callback():
$content = preg_replace_callback('/(plugin_[^ ]+)/', 'getPlugin', $content);
function getPlugin(array $matches) {
$plugin = $matches[1];
$url = "http://".$_SERVER['HTTP_HOST']."/{$plugin}.php?language=en";
//$url = "http://".$_SERVER['HTTP_HOST']."/plugin_contact.php?language=en";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
How can I get the link address after a URL has been redirected?
Take for example this URL: http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69
How can I make a PHP script echo the final URL? (http://www.eltoftnielsen.dk/default.aspx?side=sagsvisning&AutoID=125125&DID=140 in this case)
Note: The following solution isn't ideal for high traffic situations.
$url = 'http://www.boligsiden.dk/viderestilling/992cff55882a40f79e64b0a25e847a69';
file_get_contents($url);
preg_match('/(Location:|URI:)(.*?)\n/', implode("\n", $http_response_header), $matches);
if (isset($matches[0]))
{
echo $matches[0];
}
Here's what happens: file_get_contents() redirects and downloads the target website but writes the original response header into $http_response_header.
the preg_match tries to find the first "Location: x" match and returns it.
use this
<?php
$name="19875379";
$url = "http://www.ikea.co.il/default.asp?strSearch=".$name;
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$header = curl_exec($ch);
$redir = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
//print_r($header);
$x = preg_match("/<script>location.href=(.|\n)*?<\/script>/", $header, $matches);
$script = $matches[0];
$redirect = str_replace("<script>location.href='", "", $script);
$redirect = "http://www.ikea.co.il" . str_replace("';</script>", "", $redirect);
echo $redirect;
?>
enter link description here
I am making a PHP scraper and have the following piece of code that grabs the title from the page by looking inside the span uiButtonText. However I want to now scan for a hyperlink and have it pregmatch (.*).
The stars I want to be wild cards so that I can get the hyperlink from the page even if the href and onclick changes for each one.
if (preg_match("/<span class=\"uiButtonText\">(.*)<\/span>/i", $cache, $matches)){print($matches[1] . "\n");}else {}
My Full Code:
<?php
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
$url = "http://www.facebook.com/MauiNuiBotanicalGardens/info";
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html = curl_exec($ch);
$cache = $html;
if (preg_match("/<span class=\"uiButtonText\">(.*)<\/span>/i", $cache, $matches)) {print($matches[1] . "\n");}else {}
?>`
if you want to stick with your regex, try this:
$html = '<span class="uiButtonText">Google!</span>';
preg_match("/<span class=\"uiButtonText\"><a href=\".*\" class=\"thelink\" onclick=\".*\">(.*)<\/a><\/span>/i", $html, $matches);
print_r($matches[1]);
Output: Google!
A better way would be to use PHP Simple HTML DOM Parser and doing something like this:
$html = file_get_html("http://www.facebook.com/MauiNuiBotanicalGardens/info");
foreach($html->find("a.thelink") as $link){
echo $link->innertext . "<BR>";
}
Above is not tested, but should work