Skip to content

Instantly share code, notes, and snippets.

@iNaD
Last active February 29, 2024 09:37
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save iNaD/c2c19bcc9bb9e04b1fe8 to your computer and use it in GitHub Desktop.
Save iNaD/c2c19bcc9bb9e04b1fe8 to your computer and use it in GitHub Desktop.
Sending a GET/POST Request via PHP without CURL (fopen needs to be enabled)
<?php
$url = 'http://server.com/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);
var_dump($result);
@carlos112233
Copy link

gracias me ayudo tu código

@JOTAI
Copy link

JOTAI commented Sep 5, 2018

Me ha funcionado bien, pero me queda la duda, ¿Porque se utiliza cURL en otras aplicaciones?

@NnaDid
Copy link

NnaDid commented Feb 16, 2020

Wow!! great work bro. Many thanks as this has saved my head big time

@Nishad00
Copy link

Nishad00 commented Jun 5, 2021

Thanks, bro great work.

@xthesaintx
Copy link

Thanks it works, but it's really slow (2-3sec) in getting a response, ended up using CURLS

@lucianopalharez
Copy link

Thanks, it helped me to solve my task today :)

@abhishekdubey-salesforce

Why should one prefer this in place of cURL? Is there any pros of using it over php cURL?

@iNaD
Copy link
Author

iNaD commented Feb 29, 2024

Why should one prefer this in place of cURL? Is there any pros of using it over php cURL?

As this is from 2014, it was from a time where shared hosts had no curl available or disabled the usage as security measurement. So if you can use curl, just use it or better something like guzzle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment