Skip to content

Instantly share code, notes, and snippets.

@missoxd
Last active September 28, 2023 18:50
Show Gist options
  • Save missoxd/f99c541de77754309b70b583b55c1472 to your computer and use it in GitHub Desktop.
Save missoxd/f99c541de77754309b70b583b55c1472 to your computer and use it in GitHub Desktop.
<?php
$errno = 0;
$errstr = '';
$fp = fsockopen('165.227.126.241', 80, $errno, $errstr, 30);
if (!$fp) {
echo $errstr .'('. $errno . ')';
} else {
$out = "GET /ws/{put-cep-here}/json/ HTTP/1.1\r\n";
$out .= "Host: viacep.com.br\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
/*
$ dig viacep.com.br +short
165.227.126.241
$ php -f http-request-test.php
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Thu, 28 Sep 2023 18:47:42 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Expires: Thu, 28 Sep 2023 19:47:42 GMT
Cache-Control: max-age=3600
Pragma: public
Cache-Control: public
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Access-Control-Allow-Headers: Content-Type, X-Request-With, X-Requested-By
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 86400
db
{
"cep": "...",
"logradouro": "...",
"complemento": "",
"bairro": "...",
"localidade": "...",
"uf": "...",
"ibge": "...",
"gia": "",
"ddd": "...",
"siafi": "..."
}
0
*/
<?php
/**
* fsockopen(string $hostname,[ int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]])
* @see: http://php.net/manual/en/function.fsockopen.php
* @see: https://github.com/aligent/jirafe-magento-plugin/blob/master/app/code/community/Fooman/Jirafe/Model/JirafeTracker.php
*/
/**
* NOT TESTED BY ME YET!
*
* HTTP Request with fsockopen
* This can be used as async, because it doesn't wait to get a response
*/
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
/**
* NOT TESTED BY ME YET!
*
* HTTPS Request with fsockopen
* This can be used as async, because it doesn't wait to get a response
*/
$fp = fsockopen("ssl://www.example.com", 443, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment