问题
I got the following basic script who make a basic POST request (I just want to make it work, after I'll add more stuff) :
# Variables
$URL = 'http://******:8282/api/incoming_shipment/';
$postdata = http_build_query(
array(
'contract_id' => 'Showcare-R124276',
'shipment_from' => 'Montréal',
'shipment_to' => 'Chicago',
'shipping_time' => '2012-08-16 14:51:01',
'tracking_cie' => 'Poste Canada',
'tracking_type' => 'Standard',
'tracking_number' => 'EP645 9834 123 9773'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($URL, FALSE, $context);
print_r($result);
The result give me :
Warning: file_get_contents(http://******:8282/api/incoming_shipment/) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in D:\Inetpub\hightechhandling\api\api_push_contract.php on line 31
Fatal error: Maximum execution time of 30 seconds exceeded in D:\Inetpub\hightechhandling\api\api_push_contract.php on line 31
But When I go to the webpage with my browser, it work perfectly. I have tryed cURL and fsocketopen but that didn't work too. Any help please ? Thanks..
EDIT I added set_time_limit (500); and now the second error has disapear of course... but the first still remain.
回答1:
Ok, found the problem. Was sooo stupid. The problem is because the server that make the request (where the PHP file is), has a firewall enabled and the firewall allow only port 21, 22, 80, 3306 and 1433 for external request.
回答2:
For CentOS, using file_get_contents() to access URL on port other than 80 doesn't works with permission denied. It works only when selinux is set to "disabled" or "permissive".
回答3:
From PHP Manual
void set_time_limit ( int $seconds )
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out. If set to zero, no time limit is imposed.
回答4:
Try this:
'header' => implode("\r\n", array("Connection: close", "Content-type: application/x-www-form-urlencoded")),
(especially the connection: close bit)
Also: Can you duplicate the problem on the commanline with curl?
回答5:
If you don't want to disable selinux for security consideration, you can modify selinux policy for allow httpd to listen 8282
To list which ports allowed for httpd: semanage port -l | grep -w http_port_t
To add port 8282: semanage port -a -t http_port_t -p tcp 8282
来源:https://stackoverflow.com/questions/12059512/file-get-contents-does-not-work-on-port-8282