PHP curl为localhost url返回bool(false)但不返回外部url。 Curl在命令行中运行良好

When trying to access a file from localhost, using curl, all I get returned through var_dump is bool(false). However, if the $url is example.com, it works fine. The localhost server is set up and running fine so I can't see why this wouldn't work.

<?php
    $url = 'http://localhost:81';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    var_dump(curl_exec($ch));

    curl_close($ch);

curl_error returns: Curl error: Failed connect to localhost:81;

http://localhost:81 works fine in my browser

Also, curl http://localhost:81 works fine in command line