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
Related
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]
I am new to php , i am trying to get data from a website using curl (scraping),
UNABLE to get data from index.php to data.php, using CURLOPT_POST.. what am i doing wrong..?
followed this tutorial on youtube
index.php
<?php
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init("http://localhost/scrap_practise/data.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
?>
data.php
<?php
echo 'finlly in'; // this never echos
if(isset($_POST['name'],$_POST['age'])){
$db = new Mysqli("localhost","root","","mydb");
$name = $db->real_escape_string($_POST['name']);
$age = (int) $_POST['age'];
$query = "INSERT INTO data SET data='$name,$age'";
$db->query($query);
}
?>
Simply you have to update you index.php script with these line of code.
index.php
<?php
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init("http://localhost/scrap_practise/data.php");
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if( ! $result = curl_exec($ch))
{
trigger_error(curl_error($ch));
}
curl_close($ch);
// to see the return result uncomment the below line code.
//print_r($result);
?>
For more options or function reference see this link - http://php.net/manual/en/function.curl-exec.php
Hope this will help to resolve your issue !!
Try this code, I hope it will work for you...
<?php
$url = 'http://localhost/scrap_practise/data.php';
$data = array("name"=>"john","age"=>31);
$string = http_build_query($data);
echo $string;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result) ;
curl_close($ch);
?>
In data.php file
<?php
echo 'finlly in'; // this never echos
if(isset($_POST['name'],$_POST['age']))
{
echo "<pre>"; print_r($_POST);
}
?>
This will Output :
finlly in
Array
(
[name] => john
[age] => 31
)
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 try to post some data to another server using curl. The problem is that I get nothing on the other server.
Server1:
$a = $USER->id;
$b = $USER->username;
error_reporting(-1);
$url = 'http://remote_server/a.php';
$data = array(
'cus' => $a,
'cust' => $b
);
$postString = http_build_query($data, '', '&');
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 2);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$post = curl_exec ($ch);
curl_close ($ch);
and on Server2:
<?php
session_start();
var_dump($_POST);
?>
Of course it doesn't work, it gives array(0) {}.
So, how I will see the data on the other server?
To see the actual output of var_dump($_POST) in Server2, you must either :
Server1 side : echo $post variable
Server2 side : write the content of var_dump's result in a file :
ob_start();
var_dump($_POST);
$output = ob_get_clean();
$fp = fopen('log.txt', 'a');
fwrite($fp, $output);
fclose($fp);
Please help me out how can I make this working if it is th righ way to do it and if it not what would you suggest to post the parameters
$str = '';
for( $i = 11; $i <= 20; $i++ )
{
$str .= $i . ' ';
}
$ch = curl_init(); //http post to another server
curl_setopt($ch, CURLOPT_URL,"http://xxxx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=$username&password=$password&string=$str");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
print_r($server_output);
curl_close ($ch);
I understand that your code is correct, but you can use the following function as an helper:
<?php
$url = "http://xxxx";
$str = implode(" ", range(11,20));
$data = array("username" => $username, "password" => $password, "string" => $str);
$server_output = processURL($url, $data);
print_r($server_output);
function processURL($url, $data = array()){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec ($ch);
curl_close ($ch);
return $response;
}
Note that, I do understand that using range is a bit slower than the for loop, but I like it for its readability and cleaner code :)
If you have access to the script you call with curl, try to add:
var_dump($_POST);
and see what is printed.
I just made your code a bit better readable.
But its correct. Should works.
Try to have a look at the php_errors log file and see if it fires something.
<?php
$str = '';
for( $i = 11; $i <= 20; $i++ ) {
$str .= $i . ' ';
}
$ch = curl_init(); //http post to another server
curl_setopt($ch, CURLOPT_URL , 'http://xxxx');
curl_setopt($ch, CURLOPT_POST , 1);
curl_setopt($ch, CURLOPT_POSTFIELDS , 'username=' . $username . '&password=' . $password . '&string=' . $str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$server_output = curl_exec ($ch);
print_r($server_output);
curl_close($ch);
#Stoic Passing an array in POSTFIELDS determine the Content-type header = multipart. Could alter the response.