发送POST时遇到严重问题。在shell上使用curl,我的POST工作得很好。但是,当使用PHP curl或file_get_contents时,它根本不起作用。我从网络服务器收到500错误。
curl -X POST -H"Content-Type:application/xml" "http://myserver:8080/createItem?name=NewItem" --user root:123456 --data-binary @template.xml
而且:
$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);
而且:
$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);
我在这里做了一些荒唐的事情而且我没有看到?我没有看到shell正常卷曲的原因是完美的,但不是PHP实现。
答案 0 :(得分:1)
哼......我认为php/curl
不可能,但请尝试:
CURLOPT_POSTFIELDS => array('@template.xml'),
答案 1 :(得分:0)
有可能,看看这个:
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"@/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
来源:http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
答案 2 :(得分:0)
根据文档,标题
'header' => sprintf("Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type:application/xml",
应以\ r \ n结尾[见example 1 here]
'header' => sprintf( "Authorization: Basic %s\r\n", base64_encode('root:123456'))
. "Content-Type: application/xml\r\n",