file_get_contents on other port

杀马特。学长 韩版系。学妹 提交于 2019-12-13 15:22:28

问题


I must contact services rest on different ports by 80, but the function file_get_contents () returns an error: failed to open stream: Connection refused

$url = "http://nexusdigital.agency:81/API/....";

$result = file_get_contents($url, false);

How can I configure the reading on other ports?


回答1:


Use CURL :

<?php
$curl = curl_init('http://nexusdigital.agency/API/....'); 
curl_setopt($curl, CURLOPT_PORT, 81); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 81); 
$result = curl_exec($curl);
?>



回答2:


Well, if it doesn't work for you (whatever the reason) you can try using CURL http://php.net/curl

<?php
$tuCurl = curl_init(); 
curl_setopt($tuCurl, CURLOPT_URL, "http://nexusdigital.agency/API/...."); 
curl_setopt($tuCurl, CURLOPT_PORT , 81); 
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0); 
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds timeout

$tuData = curl_exec($tuCurl); 
curl_close($tuCurl);


来源:https://stackoverflow.com/questions/27687851/file-get-contents-on-other-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!