使用curl在php中发出POST请求

时间:2011-07-29 08:05:59

标签: php http post curl httprequest

我有以下命令在命令行上完美运行,

curl -X POST --header 'Category: network;scheme="http://schemas.ogf.org/occi/infrastructure#";class="kind";,virtualnetwork; scheme="http://schemas.opennebula.org/occi/infrastructure#";class="mixin";' --header 'X-OCCI-Attribute: occi.core.title="My Network",occi.core.summary="A short summary", opennebula.network.size=256, opennebula.network.address="192.168.0.0",opennebula.network.bridge=virbr0,opennebula.network.public=YES,opennebula.network.type=RANGED' http://localhost:4567/network/

我尝试使用php中的cURL扩展程序发出相同的请求,如下所示:

    $ch = curl_init("http://localhost:4567/network/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
header('Content-type: text/plain; charset=utf-8');
curl_setopt($ch,CURLOPT_VERBOSE,true);  
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Category : network;scheme="http://schemas.ogf.org/occi/infrastructure#",class="kind"',
        'Category : virtualnetwork; scheme="http://schemas.opennebula.org/occi/infrastructure#";class="mixin"'));

curl_setopt($ch,CURLOPT_HTTPHEADER,array('X-OCCI-Attribut : occi.core.title="Third Network"',
        'X-OCCI-Attribut : occi.core.summary="Test Net"', 
        'X-OCCI-Attribut : opennebula.network.size=256',
        'X-OCCI-Attribut : opennebula.network.address="10.10.10.0"',
        'X-OCCI-Attribut : opennebula.network.bridge=br0',
        'X-OCCI-Attribut : opennebula.network.public=YES',
        'X-OCCI-Attribut : opennebula.network.type=RANGED'
        ) );
 curl_setopt($ch, CURLOPT_POST, 1);
 $response = curl_exec($ch);

请求失败,服务器上没有添加任何内容。

我也尝试了HTTPRequest,但仍无法发出请求:

            $arg= array(
        'Category : network;scheme="http://schemas.ogf.org/occi/infrastructure#",class="kind"',
        'Category : virtualnetwork; scheme="http://schemas.opennebula.org/occi/infrastructure#";class="mixin"',
        'X-OCCI-Attribut : occi.core.title="Third Network"',
        'X-OCCI-Attribut : occi.core.summary="Test Net"', 
        'X-OCCI-Attribut : opennebula.network.size=256',
        'X-OCCI-Attribut : opennebula.network.address="10.10.10.0"',
        'X-OCCI-Attribut : opennebula.network.bridge=br0',
        'X-OCCI-Attribut : opennebula.network.public=YES',
        'X-OCCI-Attribut : opennebula.network.type=RANGED'
        ) ;

        $this->httpRequest->addPostFields($arg);

有什么建议??

我根据this link添加了以下行:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

得到了:

HTTP/1.1 400 Bad Request
Connection: close
Date: Fri, 29 Jul 2011 09:01:31 GMT
Content-Type: text/plain
Content-Length: 0
Server: OCCI/1.1

我成功了,我只需更正标题, 并删除

 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

正确的标题是:

    curl_setopt($ch, CURLOPT_HTTPHEADER,array('Category: network;scheme="http://schemas.ogf.org/occi/infrastructure#";class="kind";,virtualnetwork; scheme="http://schemas.opennebula.org/occi/infrastructure#";class="mixin";',
        'X-OCCI-Attribute: occi.core.title="My Net",occi.core.summary="A netw summary", opennebula.network.size=256, opennebula.network.address="10.0.0.0",opennebula.network.bridge=virbr0,opennebula.network.public=YES,opennebula.network.type=RANGED')); 
 curl_setopt($ch, CURLOPT_POST, 1);
 $response = curl_exec($ch);

1 个答案:

答案 0 :(得分:1)

使用wireshark调试网络流量。