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.
The header
function is for setting the header for the response. If you want to make a request to another server, try cURL.
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);
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/
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.
精彩评论