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; } }
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:
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.