Category: Utility Classes

Q: Http Client: Failed to open stream: HTTP request failed!

I want to post data to an local API written by Node.js:

In Cake's controller:

$http = new Client();
$response = $http->post('http://localhost:3000/api/generate', [
  'title' => 'This is a title of nothing',
  'author' => 'Ksmacky'
]);

And then I receive an error..

fopen(http://localhost:3000/api/generate): failed to open stream: HTTP request failed!

Can someone help me with the error message? 

SOLVED http fopen
3 Answers

Version: 3.0

user
Ksmacky

30

How about increasing the max_execution_time?

ini_set('max_execution_time', 300); //in seconds
Kristi
Kristi

137

Ksmacky:

Do you want to create a php file with phpinfo()? Search for "CURL" and see id it's enabled. It sounds like your php configuration is not allowed to use file functions.

If yes, check for php.ini. file_get_contents() is restricted from accessing URLs through the

allow_url_fopen = off

option.

Accepted by Ksmacky

If you are excepting a long response time:

$http = new Client();
$response = $http->post('http://localhost:3000/api/generate', [
  'title' => 'This is a title of nothing',
  'author' => 'Ksmacky'
], [
  'timeout' => 120 //set timeout to 2 mins. 
]);

Try to set timeout in options.

Created: 25 Sep '16

Last Reply: 26 Sep '16

Replies: 3

Views: 939

Votes: 1

Welcome to Aero Coding!

Aero Coding is a CakePHP-focused Q&A community for professional and enthusiast cake bakers. It's built and run by you as part of the community.


Join Now Tour

Download Cakephp

Start baking your own CakePHP application!


Cakephp All Versions