使用PHP发送POST不适用于curl或file_get_contents,仅适用于普通的bash CURL [英] Sending a POST with PHP doesn't work with curl or file_get_contents, just normal bash CURL

查看:66
本文介绍了使用PHP发送POST不适用于curl或file_get_contents,仅适用于普通的bash CURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在发送POST时遇到一些严重问题。在外壳上使用curl,我的POST可以完美工作。但是,在使用PHP curl或file_get_contents时,它根本不起作用。我从网络服务器收到500错误。

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

然后:

$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实现。

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.

推荐答案

哼...我认为 php / curl ,但尝试:

Hum... I don't think that's possible with php/curl, but try:

CURLOPT_POSTFIELDS => array('@template.xml'),

这篇关于使用PHP发送POST不适用于curl或file_get_contents,仅适用于普通的bash CURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆