在特定端口上使用curl调用API

I'm running my Symfony under the port 8000. But curl HTTP is on port 80.

That means my curl request is not working because the port is not okay I think.

I tried to specify the port to curl. But doing this makes the call request failed... I have no error message, it's just like pending when I try to call it.

I have already checked this issue: Unable to make php curl request with port number

My $url is like http://127.0.0.1/app_dev.php/api/someapi

here $result = $this->lib->CallAPI('POST', $url, $postfields);

Running under Symfony to find the URL, I should have 127.0.0.1:8000 But either I tried to set the port in the URL with not set curl option for the port. Either I try to use the URL without the port but setting the curl option for the port. It always failed.

Here is the function of making the call

function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();

    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);

            if ($data)
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    curl_setopt($curl,CURLOPT_PORT, 8000);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
}

}

The line curl_setopt($curl,CURLOPT_PORT, 8000); seems to be responsible for making my request pending.

Note that the API call from Postman is working, so the issue is really on the way I call it with curl.

Thanks for your help.

EDIT:

I used a little trick to see what is wrong.

instead of returning $result in callApi() I return the $curl. it returns me some HTML with inside this

<h1 class="break-long-words exception-message">Whoops, looks like something went wrong.</h1>
<p class="break-long-words trace-message">An instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface must be injected in FOS\RestBundle\View\ViewHandler to render templates.</p>

But I don't understand why defining the $curl variable make this error considering the called API is working on the postman

EDIT2 : In fact this erro rwas due to tryying to convert the curl object in json with json_decode...so nothing to do. Sorry. Issue still there then.