Make a POST request in php using curl
I have the following command that works perfectly on the command line,
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, opennebu开发者_开发问答la.network.address="192.168.0.0",opennebula.network.bridge=virbr0,opennebula.network.public=YES,opennebula.network.type=RANGED' http://localhost:4567/network/
I've tried to make the same request using cURL extension in php as follows:
$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);
The request fails and nothing is added on the server.
I've tried also the HTTPRequest but still unable to make the request:
$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);
Any suggestions ??
I've added the following line according to this link:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
and got :
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
I've succeded, I needed just to correct the headers, and remove the
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
The correct header is :
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);
Use wireshark to debug network flow.
精彩评论