Integration
Integration

Reputation: 347

how to do http get request in PHP without PECL_HTTP or CURL

I am putting up website & the domain hosting (1&1) only provide base PHP 5.4 (No external libraries like PECL_HTTP or CURL etc, No installed libraries ).

I can add PHP class files to my code. I am trying to do a Http/Https get on this URL $url ="https://www.googleapis.com/freebase/v1/search?query=chocolate&count=2";

I have tried Snoopy, & at least 6 different class libraries from PHPClasses none of them return anything (result is blank), I don't know why? But they all return page results.

Can anyone suggest a PHP class library that I can include (and NOT any installed library) which can do a simple Http/Https get & return results.

Upvotes: 1

Views: 4733

Answers (3)

prehfeldt
prehfeldt

Reputation: 2172

If you just want the content of this "file" you could use file_get_contents() (see first example). See @OkekeEmmanuelOluchukwu's comment on how to send header.

Upvotes: 2

Isaac Hildebrandt
Isaac Hildebrandt

Reputation: 1026

Your best bet for full http functionality (including headers, etc.) is to use file functions with a stream context.

From php.net:

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
          "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp)

Upvotes: 2

Raekye
Raekye

Reputation: 5131

Are you looking for the page output?

As you probably have found, include something.php?foo=bar does not work as php actually looks for a file ending in ?for=bar, but you can set all the get variables like this:

function getRequest($url, $data) {
    foreach ($data as $key => $value) {
        $_GET[$key] = $value;
    }
    include $url;
}

However, get variables on "full paths" (can't think of the proper name) work, eg include 'http://something.com/foo.php?bar=baz'. You can find examples here http://php.net/manual/en/function.include.php of the "variable passing" and get variables on a non-local-system.

Upvotes: 0

Related Questions