Reputation: 7024
I'm having some serious problems sending a POST. Using curl on the shell, my POST works perfectly. However, when using PHP curl, or file_get_contents, it doesn't work at all. I get a 500 error from the webserver.
curl -X POST -H"Content-Type:application/xml" "http://myserver:8080/createItem?name=NewItem" --user root:123456 --data-binary @template.xml
And this:
$options = array(
CURLOPT_HEADER => 1,
CURLOPT_HTTPHEADER => array("Content-Type:application/xml"),
CURLOPT_URL => "http://myserver:8080/createItem?name=" . rawurlencode("NewItem"),
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 20,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "root:123456",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => file_get_contents("template.xml"),
);
$post = curl_init();
curl_setopt_array($post, $options);
if(!$result = curl_exec($post)) {
trigger_error(curl_error($post));
}
curl_close($post);
And this:
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456')) . "Content-Type:application/xml",
'timeout' => 20,
'content' => file_get_contents("template.xml"),
),
));
$ret = file_get_contents("http://myserver:8080/createItem?name=" . rawurlencode("NewItem"), false, $context);
Am i doing something absurd here and i'm not seeing? I don't see a reason for the normal curl from the shell to work perfectly, but not the PHP implementations.
Upvotes: 1
Views: 1260
Reputation: 11999
According to the documentation, headers here
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type:application/xml",
should end with \r\n [see example 1 here]
'header' => sprintf( "Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type: application/xml\r\n",
Upvotes: 0
Reputation: 725
It IS possible, take a look at this:
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
Source: http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
Upvotes: 0
Reputation: 154603
Hum... I don't think that's possible with php/curl
, but try:
CURLOPT_POSTFIELDS => array('@template.xml'),
Upvotes: 1