0

I want to fetch some details from a website , my code is working fine in wampserver but when i uploaded the file on a real server i am not getting any result ,its showing me an empty page here is my code.

    <?php 
error_reporting(E_ALL);

$htm = my_curl($url);
echo $htm;
$dat = file_get_contents('cookies.txt');

echo "<pre>";
echo PHP_EOL . "<b>$url</b>";
echo PHP_EOL . "<i>$dat</i>";
echo PHP_EOL;


function my_curl( $url, $timeout=5, $error_report=TRUE)
{
    $curl = curl_init();


    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: timeout=5, max=100";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = ""; // BROWSERS USUALLY LEAVE BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_COOKIEFILE,     'cookies.txt' );
    curl_setopt( $curl, CURLOPT_COOKIEJAR,      'cookies.txt' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );


    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {   echo 'HELO';
        if ($error_report)
        {echo 'HELOs';
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

I have checked my php configuration everything seems to be okay. I couldn't find any answer in any other related post in Stack Overflow.

Error:

CURL FAIL: http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=11013001027&sem=07 TIMEOUT=5, CURL_ERRNO=7array(22) { ["url"]=> string(86) "http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=11013001027&sem=07" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0.001099) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } ["redirect_url"]=> string(0) "" }

51j0
  • 51
  • 1
  • 10
  • Have you tried `file_get_contents()` ? It takes a URL as an argument as well. – kkaosninja Mar 14 '15 at 15:25
  • yeah thats what i tried at the first place and its not working either – 51j0 Mar 14 '15 at 15:42
  • Either that site is taking more than five seconds to render, you are accessing the wrong IP/port, or you don't have a clear path between this script and the server. Do you need to open a firewall? – halfer Mar 15 '15 at 13:10
  • @halfer i am kind of new to curl concept i am trying to fetch my marksheet from university website.. my code is working fine on wampserver.. can you explain why my code isnt working on a real server. can i make it work – 51j0 Mar 15 '15 at 13:18
  • Your real server does not seem to be able to reach this IP address. Maybe there is a firewall in the way, maybe it is prohibited from making any external connections at all. Try a simple fetch on any public website of your choice, to see if that works. – halfer Mar 15 '15 at 13:23
  • @halfer Yes I am able to fetch google.com – 51j0 Mar 15 '15 at 13:25
  • OK, I expect there is a firewall rule allowing traffic on port 80 (which Google uses) but not port 7001, which is not a standard web port. Contact the adminstrator of this web server to see if they will open the port you need. They may deny this request for security reasons, however. – halfer Mar 15 '15 at 13:29
  • @halfer So if my server administrator denny my request is there any other way from which i can fetch data – 51j0 Mar 15 '15 at 13:35
  • Is it a shared host? I suspect this would be a common configuration on shared servers. The most reliable way to do this would be to have your own server so you can configure the firewall as you wish. A small VPS can do this, and costs next to nothing these days. – halfer Mar 15 '15 at 13:51
  • okay basically my university server isnt denying my request ? its my own server who isnt allowing me to fetch data – 51j0 Mar 15 '15 at 14:03

2 Answers2

0

Try setting the port seperately.

curl_setopt($ch, CURLOPT_PORT, 7001);

smottt
  • 3,272
  • 11
  • 37
  • 44
PrimuS
  • 2,505
  • 6
  • 33
  • 66
0

I think your URL is invalid - at the moment your url is something like:

http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspxrollno=123&sem=456

Whilst I assume it should be:

http://14.139.236.46:7001/Forms/Student/PrintReportCard.aspx?rollno=123&sem=456

Add the ? between aspx and the rollno and try again

bhttoan
  • 2,641
  • 5
  • 42
  • 71