Jump to content

cURL Web Fetching Experiments


phpsane

Recommended Posts

Folks,

 

Usually cURL works on my xampp. So, why not tonight ? I see white blank page!

 

 

 

<?php
 
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
 
$url='https://oscarliang.com';
$ch=curl_init();
$timeout=5;
 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 
// Get URL content
$lines_string=curl_exec($ch);
// close handle to release resources
curl_close($ch);
//output, you can also save it locally on the server
echo $lines_string;
?>
Link to comment
Share on other sites

Php Experts,

 

Below, I try getting cURL to fetch a page.

Then, I try grabbing all the words found on the page onto array to check each word against a list of banned words.

But I see complete blank page. Why is that ? I can't see any errors on my code. Do you ?

 

 

 

<?php
 
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
    $url = "https://www.google.com/search?q=dirty+harry&oq=dirty+harry&aqs=chrome..69i57.2604j0j7&sourceid=chrome&ie=UTF-8";
 
/* Youtube Channel: Clever Techie; Video: PHP cURL Tutorial - Learn PHP Programming. 
https://www.youtube.com/watch?v=1X2-UEUqrd8&t=21s
*/
 
 
// 1). $curl is going to be data type curl resource.
$curl = curl_init();
 
$search_string = "movies 2017";
$url = "http://www.tcm.com/this-month/article/297159%7C0/Dirty-Harry.html";
 
// 2). Set cURL options.
curl_setopt($curl, CURLOPT_URL, 'https://www.amazon.com');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 
// 3). Run cURL (execute http request).
$result = curl_exec($curl);
 
//Use either one of the following 4 codes. (Use one by one in order to test and learn).
 
//code 1:
//print_r($matches);
 
//code 2:
//$images = array_unique($matches[0]);
//print_r($images);
 
//code 3:
/*$images = array_values(array_unique($matches[0]));
print_r($images);
*/
 
//code 4:
/*$images = array_values(array_unique($matches[0]));
for ($i = 0; $i < count ($images); $i++) {
    echo "<div style='float:left; margin: 10 0 0 0; '>";
    echo "img src='$images[$i]'><br />";
echo "</div>";
}
*/
 
 
// 4). Close cURL resource.
curl_close($curl);
 
//Set banned words.
$banned_words = array("dirty harry","callahan", "clint eastwood");
$content = "$result";
 
//Separate each words found on the cURL fetched page.
$pieces = explode(" ", $content);
echo $pieces[0]; // 1st word
echo $pieces[1]; // 2nd word
echo $pieces[2]; // 3rd word
echo $pieces[3]; // 4th word
echo $pieces[4]; // 5th word
echo $pieces[5]; // 6th word
echo $pieces[6]; // 7th word
echo $pieces[7]; // 8th word
echo $pieces[8]; // 9th word
echo $pieces[9]; // 10th word
 
 
foreach ($banned_words as $ban) {
    if (stripos($pieces,$ban) > -1){
        echo "Script 3a - Match: $ban<br>";
//Place "exit;" if you want it to stop after finding a match.
    }else{
        echo "Script 3a - No Match: $ban<br>";
    }
}
 
 
?>
<br>
Link to comment
Share on other sites

Even this code is showing a complete white blank page on my xampp!

What is wrong ?

cURL does work on my end as I have other cURL scripts (which I wrote) working.

But the following code and the 2 mentioned on my previous posts (which I grabbed from 3rd party sites) don't work! What is this new mystery, folks! ?

 
 
<?php
 
/*
ERROR HANDLING
*/
declare(strict_types=1);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
 
    function curl_download($Url){
  // is cURL installed yet?
  if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
  }
  
  $Url = "http://www.ebay.com";
 
  // OK cool - then let's create a new cURL resource handle
  $ch = curl_init();
 
  // Now set some options (most are optional)
 
  // Set URL to download
  curl_setopt($ch, CURLOPT_URL, $Url);
 
  // User agent
  curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
 
  // Include header in result? (0 = yes, 1 = no)
  curl_setopt($ch, CURLOPT_HEADER, 0);
 
  // Should cURL return or print out the data? (true = retu rn, false = print)
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
  // Timeout in seconds
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
 
  // Download the given URL, and return output
  $output = curl_exec($ch);
 
  // Close the cURL resource, and free system resources
  curl_close($ch);
 
  return $output;
}
 
?>
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.