Skip to content

Connection always fails the first time with some URLs #10

@NSDrowned

Description

@NSDrowned

I'm using urlresolver in a piece of code that retrieves general info from a URL to provide a preview. I loop through all the domains in a string and use urlresolver on each domain before processing it. The problem is that the first domain keeps failing even if the next domain is exactly the same. The next instances of the domain will resolve fine.

To make it a bit clearer:

Domain list:

  1. www.github.com
  2. www.github.com
  3. www.github.com

Results:

  1. Connection failed
  2. Success!
  3. Success!

You can run my test here (sorry for the adult domain):
https://namethatpornstar.com/test-urlresolver.php?id=3028462

And here is the quick function I'm using on that link to show the issue.

if(!function_exists('testResolver')){
	function testResolver($string) {
        
		$resolver = new mattwright\URLResolver();
		$resolver->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36');
		$resolver->setCookieJar('/tmp/url_resolver.cookies');
		$resolver->setRequestTimeout(10);
		
		$regex = '/https?\:\/\/[^\" \n]+/i';
		preg_match_all($regex, $string, $urlarray);
		$urlarray[0] = array_unique($urlarray[0]);
		$urls = array();

		print_r("<pre>");
		print_r($urlarray);

		foreach ($urlarray[0] as &$url) {

			$url_result = $resolver->resolveURL($url);
 
			if($url_result->didConnectionFail()) {
				echo "Connection failed" . "<br>";
			}

			if ($url_result->didErrorOccur()) {
				echo $url_result->getErrorMessageString();
			} else {
				echo "Success!" . "<br>";
			}
		}
	}
}

I guess it must be something simple I'm overlooking because it only fails this way on some domains.

Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions