Monday, March 28, 2016

curl url(custum port) html grab show blank on live site php

Leave a Comment

I want to grab html of URL with custom port(instead of 8080) i.e abc.com:1234 but its shows blank on live server but works fine on localhost

My Code:

     <?php ini_set("display_errors",1); error_reporting(E_ALL);                          ini_set('allow_url_fopen', true); ini_set('allow_url_include', true); ini_set('allow_url_include', 'on'); ?>                       <?php   //getSslPage("http://portquiz.net:8080");           getSslPage("http://portquiz.net:666");                           function getSslPage($url) {           $curl_connection =   curl_init($url);          //set options ///         curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 3990);                          curl_setopt($curl_connection, CURLOPT_USERAGENT,   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");                          curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array(                              'Content-Type: text/xml',                             'Connection: Keep-Alive',                             'Keep-Alive: 300' ));                          curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);     curl_setopt($curl_connection,CURLOPT_SSL_VERIFYHOST,0);      curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);      curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);      curl_setopt($curl_connection, CURLOPT_TIMEOUT, 20); //old value 15               //set data to be posted              //curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);              //perform our request               $result = curl_exec($curl_connection);               echo "<h1 style=color:blue>Output Result:</h1><br>";               echo htmlentities($result);                echo "<h1 style=color:blue>|Curl Exec Detail:</h1><br>";               //var_dump($result);               echo "<pre>";              print_r(curl_getinfo($curl_connection));                echo "</pre>";                echo curl_errno($curl_connection) . '<br/>';              echo curl_error($curl_connection) . '<br/>';                             } exit; ?> 

Output on Localhost:

Output Result:  <html> <head> <title>Outgoing Port Tester</title> <style type="text/css"> body { font-family: sans-serif; font-size: 0.9em; } </style> </head> <body> <h1>Outgoing port tester</h1> This server listens on all TCP ports, allowing you to test any outbound TCP port. <p> You have reached this page on port <b>666</b>.<br/> </p> Your network allows you to use this port. (Assuming that your network is not doing advanced traffic filtering.) <p> Network service: unknown<br/> Your outgoing IP: 39.32.94.166</p> <h2>Test a port using a command</h2> <pre> $ telnet portquiz.net 666 Trying ... Connected to portquiz.net. Escape character is '^]'. </pre> <pre> $ nc -v portquiz.net 666 Connection to portquiz.net 666 port [tcp/daytime] succeeded! </pre> <pre> $ curl portquiz.net:666 Port 666 test successful! Your IP: 39.32.94.166</pre> <pre> $ wget -qO- portquiz.net:666 Port 666 test successful! Your IP: 39.32.94.166</pre> <pre> # For Windows PowerShell users PS C:\&gt; Test-NetConnection -InformationLevel detailed -ComputerName portquiz.net -Port 666</pre> <h2>Test a port using your browser</h2> <p> In your browser address bar: <strong>http://portquiz.net:XXXX</strong> </p> Examples: <br/> <a href="http://portquiz.net:8080">http://portquiz.net:8080</a> <br/> <a href="http://portquiz.net:8">http://portquiz.net:8</a> <br/> <a href="http://portquiz.net:666">http://portquiz.net:666</a> <br/> <p> I got complains that portquiz is not working on port 445. My hosting company OVH is probably blocking this port. Sorry about that. Feel free to contact them. See <a href="http://positon.org/is-ovh-blocking-port-445">my blog post</a> and <a href="https://forum.ovh.com/showthread.php/106901-OVH-bloque-le-port-445-vers-mon-serveur-d%C3%A9di%C3%A9">OVH forum post (french)</a>. </p> <p> Your browser can block network ports normally used for purposes other than Web browsing. In this case you should use the telnet or netcat commands to test the port. </p> <p> Please also note that this server uses some port for real services (22, 25), so testing with your browser on those ports will not work. </p> <p> <i>Contact/feedback:</i><br/> <img src="portquizm.png" /> </p> <p> <i>See also:</i><br/> <ul> <li> <a href="http://en.positon.org/post/An-outgoing-port-tester">Blog post on this topic</a> and <a href="http://positon.org/portquiz-net-how-it-works">How it works</a> </li> <li> <a href="http://www.firebind.com/">Firebind</a>, a commercial tester. <a href="http://www.firebind.com/clients/web/">javascript test</a> </li> <li> <a href="https://github.com/nhooyr/outPorts">outPorts</a>, a tiny program to test a range of ports using portquiz </li> </ul> </p> </body> </html> |Curl Exec Detail:  Array (     [url] => http://portquiz.net:666/     [content_type] => text/html     [http_code] => 200     [header_size] => 233     [request_size] => 184     [filetime] => -1     [ssl_verify_result] => 0     [redirect_count] => 0     [total_time] => 0.5     [namelookup_time] => 0     [connect_time] => 0.234     [pretransfer_time] => 0.234     [size_upload] => 0     [size_download] => 2694     [speed_download] => 5388     [speed_upload] => 0     [download_content_length] => 2694     [upload_content_length] => 0     [starttransfer_time] => 0.5     [redirect_time] => 0     [redirect_url] =>      [primary_ip] => 178.33.250.62     [certinfo] => Array         (         )      [primary_port] => 666     [local_ip] => 192.168.1.47     [local_port] => 54754 )  0 

Output on Live Site:(result show blank/Empty)

Output Result:  |Curl Exec Detail:  Array (     [url] => http://portquiz.net:666/     [content_type] =>      [http_code] => 0     [header_size] => 0     [request_size] => 0     [filetime] => -1     [ssl_verify_result] => 0     [redirect_count] => 0     [total_time] => 19.340628     [namelookup_time] => 0.117558     [connect_time] => 0     [pretransfer_time] => 0     [size_upload] => 0     [size_download] => 0     [speed_download] => 0     [speed_upload] => 0     [download_content_length] => -1     [upload_content_length] => -1     [starttransfer_time] => 0     [redirect_time] => 0     [redirect_url] =>      [primary_ip] =>      [certinfo] => Array         (         )      [primary_port] => 0     [local_ip] =>      [local_port] => 0 )  28 Connection timed out after 20001 milliseconds 

Please Help me i am working it from last 2 days but no success thanks

5 Answers

Answers 1

In the example you posted above i saw no curl_exec, so i added that.

This works for me:

<?php ini_set("display_errors", 1); error_reporting(E_ALL);  ini_set('allow_url_fopen', true); ini_set('allow_url_include', true); ini_set('allow_url_include', 'on'); ?>  <?php  //getSslPage("http://portquiz.net:8");  function getSslPage($url) {     $curl_connection = curl_init($url); //set options ///curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 3990);      curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");      curl_setopt($curl_connection, CURLOPT_HTTPHEADER, array(         'Content-Type: text/xml',         'Connection: Keep-Alive',         'Keep-Alive: 300'));      curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);     curl_setopt($curl_connection, CURLOPT_SSL_VERIFYHOST, 0);     curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);     curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);     curl_setopt($curl_connection, CURLOPT_TIMEOUT, 20); //old value 15 //set data to be posted //curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); //perform our request  $result = curl_exec($curl_connection);  echo "<h1 style=color:blue>Output Result:</h1><br>";  echo htmlentities($result);   echo "<h1 style=color:blue>|Curl Exec Detail:</h1><br>";  //var_dump($result);  echo "<pre>";     echo "</pre>";     echo curl_errno($curl_connection) . '<br/>';     echo curl_error($curl_connection) . '<br/>';      print_r(curl_exec($curl_connection));      print_r(curl_getinfo($curl_connection)); }  getSslPage("http://portquiz.net:666"); 

result:

</pre>0<br/><br/> <html> <head> <title>Outgoing Port Tester</title> <style type="text/css"> body {     font-family: sans-serif;     font-size: 0.9em; } </style>  </head>  <body> <h1>Outgoing port tester</h1>  This server listens on all TCP ports, allowing you to test any outbound TCP port.  <p> You have reached this page on port <b>666</b>.<br/> </p>  Your network allows you to use this port. (Assuming that your network is not doing advanced traffic filtering.)  <p> Network service: unknown<br/> Your outgoing IP: 131.228.182.254</p>  <h2>Test a port using a command</h2>  <pre> $ telnet portquiz.net 666  Trying ... Connected to portquiz.net. Escape character is '^]'. </pre> <pre> $ nc -v portquiz.net 666  Connection to portquiz.net 666 port [tcp/daytime] succeeded! </pre> <pre> $ curl portquiz.net:666  Port 666 test successful! Your IP: 131.228.182.254</pre> <pre> $ wget -qO- portquiz.net:666  Port 666 test successful! Your IP: 131.228.182.254</pre> <pre> # For Windows PowerShell users PS C:\&gt; Test-NetConnection -InformationLevel detailed -ComputerName portquiz.net -Port 666</pre>  <h2>Test a port using your browser</h2>  <p> In your browser address bar: <strong>http://portquiz.net:XXXX</strong> </p>  Examples: <br/> <a href="http://portquiz.net:8080">http://portquiz.net:8080</a> <br/> <a href="http://portquiz.net:8">http://portquiz.net:8</a> <br/> <a href="http://portquiz.net:666">http://portquiz.net:666</a> <br/>  <p> I got complains that portquiz is not working on port 445. My hosting company OVH is probably blocking this port. Sorry about that. Feel free to contact them. See <a href="http://positon.org/is-ovh-blocking-port-445">my blog post</a> and <a href="https://forum.ovh.com/showthread.php/106901-OVH-bloque-le-port-445-vers-mon-serveur-d%C3%A9di%C3%A9">OVH forum post (french)</a>. </p>  <p> Your browser can block network ports normally used for purposes other than Web browsing. In this case you should use the telnet or netcat commands to test the port. </p> <p> Please also note that this server uses some port for real services (22, 25), so testing with your browser on those ports will not work. </p>   <p> <i>Contact/feedback:</i><br/> <img src="portquizm.png" /> </p>  <p> <i>See also:</i><br/> <ul> <li> <a href="http://en.positon.org/post/An-outgoing-port-tester">Blog post on this topic</a> and <a href="http://positon.org/portquiz-net-how-it-works">How it works</a> </li> <li> <a href="http://www.firebind.com/">Firebind</a>, a commercial tester. <a href="http://www.firebind.com/clients/web/">javascript test</a> </li> <li> <a href="https://github.com/nhooyr/outPorts">outPorts</a>, a tiny program to test a range of ports using portquiz </li> </ul> </p>  </body>  </html> Array (     [url] => http://portquiz.net:666/     [content_type] => text/html     [http_code] => 200     [header_size] => 233     [request_size] => 184     [filetime] => -1     [ssl_verify_result] => 0     [redirect_count] => 0     [total_time] => 0.054183     [namelookup_time] => 0.004118     [connect_time] => 0.024217     [pretransfer_time] => 0.024252     [size_upload] => 0     [size_download] => 2703     [speed_download] => 49886     [speed_upload] => 0     [download_content_length] => 2703     [upload_content_length] => -1     [starttransfer_time] => 0.053941     [redirect_time] => 0     [redirect_url] =>      [primary_ip] => 178.33.250.62     [certinfo] => Array     (     )      [primary_port] => 666     [local_ip] => 10.223.128.174     [local_port] => 36696 ) 

Answers 2

The code seems fine to me, as a workaround try changing :

curl_setopt ( $curl_connection, CURLOPT_RETURNTRANSFER, true );

to

curl_setopt ( $curl_connection, CURLOPT_RETURNTRANSFER, 1 );

As described in the Return Values section of curl-exec PHP manual page: http://php.net/manual/function.curl-exec.php

You should enable the CURLOPT_FOLLOWLOCATION option for redirects but this would be a problem if your server is in safe_mode and/or open_basedir is in effect which can cause issues with curl as well.

Answers 3

The important message here is:

Connection timed out after 20001 milliseconds

It means your connection didn't go through to that host. The reason maybe a firewall or network misconfiguration on either end - your server or remote server.

The first thing that comes to my mind is port number (666) that is lower than 1024. On Unix systems only privileged users can listen to those ports. So if you are using regular user you could be unable to listen to it.

But if you are able to connect to it from your local machine the problem is probably is with your server. In order to check if it is configured to pass connections to any port you can try this simple command in your terminal:

$ telnet portquiz.net 666 Trying 178.33.250.62... Connected to portquiz.net. Escape character is '^]'. GET / 

GET / is HTTP request you need to enter from keyboard if/when you get the prompt. If you don't get the prompt and you get timeout message after some time - your host is configured to not pass connections to custom ports. This is often the issue with shared hosting.

In such a case you should do one of the following:

  • reconfigure your host to allow such connections
  • use some standard port: 80, 8080, 443, etc
  • switch your hosting provider to the one that doesn't have such restrictions.

EDIT: You can also do a simple check from your PHP script:

$fp = fsockopen("portquiz.net", 666); var_dump($fp); 

If result of var_dump is false or you get a timeout executing that page - the reason is connecting to random ports is not permitted on your hosting.

Answers 4

Note that curl_* functions in PHP are wrapper of curl program.

It means you can try same thing via command line.

If you can connect your live server via SSH or Telnet,

Try curl http://portquiz.net:666 -v and see what the real problem is.

Answers 5

Have you noticed this error "Connection timed out after 20001 milliseconds"

Increase your Connection time out time here add enough time instead of 20

curl_setopt($curl_connection, CURLOPT_TIMEOUT, 20); 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment