OhkaBaka
OhkaBaka

Reputation: 349

How do I set a http header variable for sending

I'm building a testing tool and I need to be able to send a variable in a http request to my ingester, which should be working just fine.

The problem is it is dreadfully easy to figure out how to READ 'Accept-Monkeys: Basic capuchin; rhesus' but it like pulling teeth to find a way to SEND it...

Basically I want the PHP equivalent of the ColdFusion CFHTTPPARAM:

<cfhttp method="get" url="http://127.0.0.1/monkeycheck" >
  <cfhttpparam name="Accept-Monkeys" type="Header" value="Basic Capuchin; Rhesus">
</cfhttp>

Ideally I don't want the content, I want to GO there... but if I have too, I would take a CFHTTP response and display it.

(Incidentally, this is in Drupal, if it matters, but I read [possibly inaccurately] that there is no way to do this in Drupal)

Upvotes: 0

Views: 90

Answers (2)

Sammitch
Sammitch

Reputation: 32272

cURL:

$ch = curl_init('http://monkeymaster.com/getmonkeys.php');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Accept-Monkeys: Basic Capuchin; Rhesus',
  'Another-Header: yep'
));
curl_exec($ch);

Stream Wrapper:

$opts = array(
  'http'=>array(
    'header'=>"Accept-Monkeys: Basic Capuchin; Rhesus\r\n" .
              "Cookie: foo=bar\r\n"
  )
);
$context = stream_context_create($opts);

$fp = fopen('http://monkeymaster.com/getmonkeys.php', 'r', false, $context);
fpassthru($fp);
fclose($fp);

Upvotes: 1

Shaggy
Shaggy

Reputation: 233

Look into the header function.

header("Accept-Monkeys : Basic Capuchin; Rhesus");

http://www.php.net/manual/en/function.header.php

Upvotes: 0

Related Questions