Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:57:27+00:00 2026-06-05T00:57:27+00:00

I want to make HTTP request without having dependency to cURL and allow_url_fopen =

  • 0

I want to make HTTP request without having dependency to cURL and allow_url_fopen = 1 by opening socket connection and send raw HTTP request:

/**
 * Make HTTP GET request
 *
 * @param   string   the URL
 * @param   int      will be filled with HTTP response status code
 * @param   string   will be filled with HTTP response header
 * @return  string   HTTP response body
 */
function http_get_request($url, &$http_code = '', &$res_head = '') 
{
  $scheme = $host = $user = $pass = $query = $fragment = '';
  $path = '/';
  $port = substr($url, 0, 5) == 'https' ? 443 : 80;

  extract(parse_url($url)); 

  $path .= ($query ? "?$query" : '').($fragment ? "#$fragment" : '');

  $head = "GET $path HTTP/1.1\r\n"
        . "Host: $host\r\n"
        . "Authorization: Basic ".base64_encode("$user:$pass")."\r\n"
        . "Connection: close\r\n\r\n";

  $fp = fsockopen($scheme == 'https' ? "ssl://$host" : $host, $port) or 
    die('Cannot connect!');

  fputs($fp, $head);
  while(!feof($fp)) {
    $res .= fgets($fp, 4096);
  }
  fclose($fp);

  list($res_head, $res_body) = explode("\r\n\r\n", $res, 2);
  list(, $http_code, ) = explode(' ', $res_head, 3);

  return $res_body;
}

The function works ok, but since I’m using HTTP/1.1, the response body usually returned in Chunked-encoded string. For example (from Wikipedia):

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

I don’t want to use http_chunked_decode() since it has PECL dependency and I want a highly portable code.

How to easily decode HTTP-chunked encoded string so my function can return the original HTML? I also have to make sure that the length of the decoded string match with the Content-Length: header.

Any help would be appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:57:29+00:00Added an answer on June 5, 2026 at 12:57 am

    Since the function returns the HTTP response header, you should check if 'Transfer-Encoding' is 'chunked' then decode the chunked-encoded string.
    In pseudocode:

    CALL parse_http_header
    IF 'Transfer-Encoding' IS 'chunked'
      CALL decode_chunked
    

    Parsing HTTP response header:

    Below is the function to parse HTTP response header to associative array.

    function parse_http_header($str) 
    {
      $lines = explode("\r\n", $str);
      $head  = array(array_shift($lines));
      foreach ($lines as $line) {
        list($key, $val) = explode(':', $line, 2);
        if ($key == 'Set-Cookie') {
          $head['Set-Cookie'][] = trim($val);
        } else {
          $head[$key] = trim($val);
        }
      }
      return $head;
    }
    

    The function will return an array like this:

    Array
    (
        [0] => HTTP/1.1 200 OK
        [Expires] => Tue, 31 Mar 1981 05:00:00 GMT
        [Content-Type] => text/html; charset=utf-8
        [Transfer-Encoding] => chunked
        [Set-Cookie] => Array
            (
                [0] => k=10.34; path=/; expires=Sat, 09-Jun-12 01:58:23 GMT; domain=.example.com
                [1] => guest_id=v1%3A13; domain=.example.com; path=/; expires=Mon, 02-Jun-2014 13:58:23 GMT
            )
        [Content-Length] => 43560
    )
    

    Notice how the Set-Cookie headers parsed to array. You need to parse the cookies later to associate a URL with the cookies need to be sent.


    Decode the chunked-encoded string

    The function below take the chunked-encoded string as the argument, and return
    the decoded string.

    function decode_chunked($str) {
      for ($res = ''; !empty($str); $str = trim($str)) {
        $pos = strpos($str, "\r\n");
        $len = hexdec(substr($str, 0, $pos));
        $res.= substr($str, $pos + 2, $len);
        $str = substr($str, $pos + 2 + $len);
      }
      return $res;
    }
    
    // Given the string in the question, the function above will returns:
    //
    // This is the data in the first chunk
    // and this is the second one
    // consequence
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I want to make a http request with a body (say file, file2,
Please find the code at http://jsfiddle.net/wlogeshwaran/NGL8P/4/ Here i want to make the 'hi' ,
I am using http://mjsarfatti.com/sandbox/nestedSortable/ Nested Sortables for JQuery. I want to make each nestable
i'm new to gooogle map developing.i want to make a map as follows. http://edition.cnn.com/SPECIALS/world/arab-unrest/index.html
I need to make an HTTP POST request from outside the browser, but the
I wish to make a simple GET request to another script on a different
I want make datetimepicker in my project. Using jquery how it is possible? I
I want make a bash script which returns the position of an element from
I want make interactive application where user launches it and can do various task
Let's say I want make some of my sources publicly available via my blog

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.