Trying to open an external page using php

  • barry
  • Graduate
  • Graduate
  • User avatar
  • Posts: 115
  • Loc: scotland

Post 3+ Months Ago

Can some one please tell me why this works

Code: [ Select ]
function get_type(){
    $site = fopen('http://www.city-visitor.com/dundee/plumbers.html','r'); // i have added the url here
    while($cont = fread($site,1024657)){
        $total .= $cont;
    }
    fclose($site);
    $match_expression = '/<h1 class="Listing">(.*)<\/h1>/Us';
    preg_match($match_expression,$total,$matches);
    return $matches[1];
}

$type = get_type();

echo $type;
  1. function get_type(){
  2.     $site = fopen('http://www.city-visitor.com/dundee/plumbers.html','r'); // i have added the url here
  3.     while($cont = fread($site,1024657)){
  4.         $total .= $cont;
  5.     }
  6.     fclose($site);
  7.     $match_expression = '/<h1 class="Listing">(.*)<\/h1>/Us';
  8.     preg_match($match_expression,$total,$matches);
  9.     return $matches[1];
  10. }
  11. $type = get_type();
  12. echo $type;


BUT this dosnt work

Code: [ Select ]

$url = 'http://www.city-visitor.com/dundee/plumbers.html';

function get_type(){
    $site = fopen($url,'r'); // this time im calling the url from the $url from the variable two lines back
    while($cont = fread($site,1024657)){
        $total .= $cont;
    }
    fclose($site);
    $match_expression = '/<h1 class="Listing">(.*)<\/h1>/Us';
    preg_match($match_expression,$total,$matches);
    return $matches[1];
}


$type = get_type();

echo $type;
  1. $url = 'http://www.city-visitor.com/dundee/plumbers.html';
  2. function get_type(){
  3.     $site = fopen($url,'r'); // this time im calling the url from the $url from the variable two lines back
  4.     while($cont = fread($site,1024657)){
  5.         $total .= $cont;
  6.     }
  7.     fclose($site);
  8.     $match_expression = '/<h1 class="Listing">(.*)<\/h1>/Us';
  9.     preg_match($match_expression,$total,$matches);
  10.     return $matches[1];
  11. }
  12. $type = get_type();
  13. echo $type;




I get an error saying this is not a valid resource
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Your not passing the variable $url into the function.

Code: [ Select ]
// Change this
function get_type(){
// To this
function get_type($url){
  1. // Change this
  2. function get_type(){
  3. // To this
  4. function get_type($url){
  • dark_lord
  • Graduate
  • Graduate
  • User avatar
  • Posts: 162
  • Loc: India-Kolkata

Post 3+ Months Ago

or use global variable

Code: [ Select ]
function get_type(){
    $site = fopen($url,'r'); // this time im calling the url
  1. function get_type(){
  2.     $site = fopen($url,'r'); // this time im calling the url


to
Code: [ Select ]
function get_type(){
global $url;
    $site = fopen($url,'r'); // this time im calling the url
  1. function get_type(){
  2. global $url;
  3.     $site = fopen($url,'r'); // this time im calling the url

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.