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 6749271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:40:13+00:00 2026-05-26T12:40:13+00:00

If cURL is unavailable I want to send HTTP requests using fopen. I got

  • 0

If cURL is unavailable I want to send HTTP requests using fopen. I got the code for a class from a PACKT RESTful PHP book but it does nto work. Any ideas why?

if ($this->with_curl) {     
  //blah
} else {    
    $opts = array (
            'http' => array (
            'method' => "GET",
            'header'  => array($auth,
            "User-Agent: " . RESTClient :: USER_AGENT . "\r\n"),
            )
    );
    $context = stream_context_create($opts);
    $fp = fopen($url, 'r', false, $context);
    $result = fpassthru($fp);
    fclose($fp);
    }

    return $result;
}
  • 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-05-26T12:40:14+00:00Added an answer on May 26, 2026 at 12:40 pm

    The HTTP context options are laid out here: http://www.php.net/manual/en/context.http.php

    The header option is a string, so as @Mob says you should be using \r\n and string concatenation rather than an array. However, user_agent is a valid key, so you could just use that instead.

    I’m guessing that the contents of the $auth variable is something along the lines of Authorization: blah – i.e. standard header format?

    The below code is a working example. Note that I’ve changed your fpassthru() (which outputs the content to the browser, and does not store it to $result) to a fread() loop. Alternatively you could have wrapped the fpassthru() call with ob_start(); and $result = ob_get_clean();

    <?php
    class RESTClient {
      const USER_AGENT = 'bob';
    }
    $url = 'http://www.example.com/';
    $username = "fish";
    $password = "paste";
    $b64 = base64_encode("$username:$password");
    $auth = "Authorization: Basic $b64";
    $opts = array (
            'http' => array (
                'method' => "GET",
                'header' => $auth,
                'user_agent' => RESTClient :: USER_AGENT,
            )
    );
    $context = stream_context_create($opts);
    $fp = fopen($url, 'r', false, $context);
    $result = "";
    while ($str = fread($fp,1024)) {
        $result .= $str;
    }
    fclose($fp);
    echo $result;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using cURL to log in, like so: curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_URL,'http://www.website.com/login.php'); curl_setopt($ch,
I'm using PHP/CURL to automate calls between 2 closely tied code igniter. Code igniter
I want use php curl with oauth to get the JSON data from twitter
I'm using curl to retrieve information from wikipedia. So far I've been successful in
I was using curl from shell scripts and was setting -s option to make
I'm using PHP Curl to measure download times for a webpage. I know this
I am using curl, I am wondering how would I send post/submit data on
I'm using CURL to import some code. However, in french, all the characters come
I'm currently using Curl for PHP a lot. It takes a lot of time
Using Curl for example, I can "post" data (send an entity-body) in a GET

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.