Senica Gonzalez
Senica Gonzalez

Reputation: 8182

PHP: Post data with Headers....not working

Simple Script

<?php
$host = "mysite.com";
$path = "/test.php";
$data = "drivingMeCrazy";
$data = urlencode($data);

header("POST $path HTTP/1.1\r\n" );
header("Host: $host\r\n" );
header("Content-type: application/x-www-form-urlencoded; charset=utf-8\r\n" );
header("Content-length: " . strlen($data) . "\r\n" );
header("Connection: close\r\n\r\n" );
header($data);
?>

When running this, my server just gives me Internal Error. All I need to do is just....POST. Nothing else. I don't need a response or anything.

Following Specs from here: http://www.sixapart.com/pronet/docs/trackback_spec.

Upvotes: 0

Views: 1970

Answers (4)

mr-sk
mr-sk

Reputation: 13407

You're looking for something like this:

$process = curl_init($host);                                                                         
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additionalHeaders));  //<-- update to reflect your content-type
curl_setopt($process, CURLOPT_HEADER, 1);                                                                           
curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password); // <-- you probably don't need that                                                
curl_setopt($process, CURLOPT_TIMEOUT, 30);                                                                         
curl_setopt($process, CURLOPT_POST, 1);                                                                             
curl_setopt($process, CURLOPT_POSTFIELDS, $payloadName);                                                            
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);                                                                
$return = curl_exec($process);  

Upvotes: 2

Senica Gonzalez
Senica Gonzalez

Reputation: 8182

Found an answer I was looking for here:

http://bytes.com/topic/php/answers/1211-doing-http-post

Curl would have worked. I just wanted to make this work on a different platform as well, so I wanted to find the most direct way. Thanks again.

Upvotes: 0

user229044
user229044

Reputation: 239311

You cannot use header() to send data to a 3rd party server. header() simply sends header information to the client making the request to your webserver.

Look into the cURL library for initiating requests to 3rd parties from your server-side PHP:

http://ca.php.net/manual/en/book.curl.php

I cannot vouch for this library, but it appears to be a freely available implementation of Trackbacks for PHP:

http://sourceforge.net/projects/phptrackback/

Upvotes: 1

Gumbo
Gumbo

Reputation: 655239

The header function is for setting the header for the response. If you want to make a request to another server, try cURL.

Upvotes: 2

Related Questions