Need help with converting cgi to php

Discussion in 'PHP' started by buffalos, Aug 29, 2009.

  1. #1
    Hi, please can someone help me convert cgi script below to php?

    sub GetLink {
    $nurl=shift;
    $id=shift;
    $ua = new LWP::UserAgent;
    $ua->agent("Mozilla/4.0");
    $req = new HTTP::Request 'GET' => $nurl;
    $req->header('Accept' => 'text/html');
    $res = $ua->request($req);
    if ($res->is_success) {
    $content=$res->headers_as_string.$res->content;
    $real_url=$res->base;
    $content=~/URL=([^\s]+)/;
    $newurl=$1;
    $real_url=~s/\?.*$//;
    } else {
    $real_url=$url;
    }

    }
     
    buffalos, Aug 29, 2009 IP
  2. wahyu

    wahyu Peon

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2

    I just give my PHP script using CURL to download URL:

    
    function get_page($url) {
    	if (function_exists('curl_init')) {
    		$ch = curl_init($url);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    	return curl_exec($ch);
    	} else {
    		return file_get_contents($url);
    	}
    }
    
    PHP:
     
    wahyu, Aug 29, 2009 IP
  3. buffalos

    buffalos Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks. Is it possible to change function to return the url, instead on web page content?
     
    buffalos, Aug 29, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You could always peform this regex as you do in the CGI script against the returned content.

    Modifying the previous poster's function to:

    
    preg_match("/URL=([^\s]+)/", $subject, $matches
    
    function get_page($url) {
        if (function_exists('curl_init')) {
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        $data = curl_exec($ch);
        } else {
            $data = file_get_contents($url);
        }
    
    preg_match("/URL=([^\s]+)/", $data, $matches);
    return $matches[0];
    }
    
    
    PHP:
    What language is your original script in (CGI only means Common Gateway Interface which could be written in a number of languages), seems Perl, but I don't know enough bout it, perhaps if you described what its supposed to do you can get a better conversion.
     
    kblessinggr, Aug 29, 2009 IP