Sadegh Sadegh - 1 month ago 11
PHP Question

how to copy a webpage text with code in php

I want to copy a page text in a variable.
I use file_get_contents method but it don't work for my address
for example my address is :

http://www.bankmellat.ir/3/Default/94/1/Default/2/875/1/201.aspx?itemid=201

but this code not show the text of this page:

$filename='http://www.bankmellat.ir/3/Default/94/1/Default/2/875/1/201.aspx?itemid=201';
$homepage = file_get_contents($filename , false);
echo ($homepage);


please help me

thanks

Answer

Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.

Use this code:

<?php
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

// INIT CURL
$ch = curl_init();

//init curl 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.bankmellat.ir/3/Default/94/1/Default/2/875/1/201.aspx?itemid=201');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

// common name and also verify that it matches the hostname provided)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Optional: Return the result instead of printing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$store = curl_exec ($ch);
echo $store;

// CLOSE CURL
curl_close ($ch);
?>

enjoy!!

Comments