I'm writing a blog in PHP and it has to make HTTP API calls to another web service. The PHP I'm using doesn't have curl support so I'm unable to use that. How do I make HTTP API calls from PHP without curl?
Asked
Active
Viewed 5,569 times
0
-
You could try [`file_get_contents()`](http://php.net/manual/en/function.file-get-contents.php) provided the request is a `GET` request. I don't know of a way to use it with a `POST` request. The returned data would be a string that you would need to parse either to JSON or some other usable form depending on the API return type. – War10ck Oct 09 '14 at 17:40
-
1@War10ck `fgc()` works with post requests just fine – PeeHaa Oct 09 '14 at 17:42
-
@War10ck I also need POST requests... – bodacydo Oct 09 '14 at 17:43
-
@PeeHaa Sorry. Unfamiliar with `wgc()`. Can you elaborate? – War10ck Oct 09 '14 at 17:43
-
I need to make POST request myself, too. – bodacydo Oct 09 '14 at 17:43
-
1@War10ck typo. Mean [`fgc()`](http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents) – PeeHaa Oct 09 '14 at 17:44
-
1http://www.php.net/fsockopen and fully imitate http requests – Cheery Oct 09 '14 at 17:46
-
@Cheery That is too low level... – bodacydo Oct 09 '14 at 17:47
-
@bodacydo but it does not require any additional extension. `file_get_contents` and other wrappers do not send full http headers. But, you also can use `wget` with command line if it is available )) Moreover, I'm sure there are classes available which do everything for you by `fsockopen` – Cheery Oct 09 '14 at 17:47
-
What about this - http://php.net/manual/en/function.http-get.php - it looks like new PHP versions have built in http client? Has anyone used this? I just found out... – bodacydo Oct 09 '14 at 17:49
-
@PeeHaa Nice. Didn't know that. Learn something new everyday. :) – War10ck Oct 09 '14 at 18:04
-
@bodacydo `fsockopen` is not low level, it is equiv to `fopen`. I'm sorry you have to form the full HTTP request header, manage response header parsing, consider body encoding, and connection pooling/keep-alives ...all by yourself. – zamnuts Oct 09 '14 at 18:38
3 Answers
2
Depends on the response from the API.
If your response comes back as XML then $response = simplexml_load_file($API_CALL)
worked for me.
If you response is json then $response = json_decode(file_get_contents($API_CALL))
should work.
also For post requests you can do this
$url = 'http://domain/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

wintheday
- 139
- 9
-
-
depends on the API all the ones I create have GET upload functions so that a call can say http://domain/API.dll?API=CREATE&XML= then my response to those are true or false+errors. Did you create the API or is it someone else's? – wintheday Oct 09 '14 at 18:00
-
FYI: This won't work if [`allow_url_fopen` is `0`](http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen). – zamnuts Oct 09 '14 at 18:12
-
0
Sounds like you're going to be writing some socket code. You'll want to refer to http://php.net/manual/en/book.sockets.php.

chaos
- 122,029
- 33
- 303
- 309
-
This looks too low level... Surely someone has solved this many times... – bodacydo Oct 09 '14 at 17:55
-
1