开发者

cURL is not posting the query string

I am not that experienced with cURL and I have spent a couple of days trying to sort this problem: I have an issue with cURL not appending the query string to my URL in the headers when I submit a POST request; hence no 'payload' is received by the server and I get returned an error status code by the service I'm accessing which indicated it didn't receive the appropriate data.

I think the POST should start with the full domain name, but I'm not sure. If I'm posting data, shouldn't Content-Length be '0' instead of what I am getting?

The outgoing header looks like this:

POST /rest/v1/oliver/groups/ORIGINNUMBER/member? HTTP/1.1
Accept: application/xml
Content-ty开发者_运维百科pe: text/plain
User-Agent: Custom PHP Script
Host: campaign.oventus.com
Cookie:  JSESSIONID=SECRETCOOKIE
Content-Length: 95

My php code looks like this:

$fields_string = "firstName=$fname&secondName=$sname&password=$pass&deviceAddress=$phonenumber&notes=testing";

$url = "http://campaign.oventus.com/rest/v1/ACCOUNTNAME/groups/ORIGINNUMBER/member?";

  $header[] = "Accept: application/xml";
  $header[] = "Content-type: text/plain";
  $header[] = "User-Agent: Custom PHP Script";
  $header[] = "Host: campaign.oventus.com";
  $header[] = "Cookie: ".$cookie;

$cx = curl_init();
    curl_setopt($cx, CURLOPT_URL,$url);
    curl_setopt($cx, CURLOPT_POST, 5);
    curl_setopt($cx, CURLOPT_POSTFIELDS,$fields_string);
    curl_setopt($cx, CURLOPT_HTTPHEADER, $header);
    curl_setopt($cx, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($cx, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($cx, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($cx, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($cx, CURLOPT_TIMEOUT, 15);
    curl_setopt($cx, CURLOPT_HEADER, 1);
    curl_setopt($cx, CURLINFO_HEADER_OUT, TRUE);
$final = curl_exec($cx);
$errors = curl_error($cx);
$errornos = curl_errno($cx);

$headcut2 = explode ("n/xml", $final);
$headstring2 = $headcut2[0]."n/xml";
$xmlstring2 = $headcut2[1];

    echo "<h2>Add to Group result: </h2>";
    echo "<p>RAW header: <code>$final</code></p>";
    //echo "<p>Response header: <code>".htmlentities($headstring2)."</code></p>";
    echo "<p>XML response: <code>".htmlentities($xmlstring2)."</code></p>";
    //echo "<p>".print_r($info)."</p>";
    //echo "<p>CURL info: $info</p>";
    //echo "<p>Curl error: $errors</p>";
    echo "<p>Curl error num: $errornos</p>";

    print "<pre>\n";
    print_r(curl_getinfo($cx));  // get error info
    print "</pre>\n";

    curl_close($cx);

And the header returned by the server is this:

HTTP/1.1 200 OK Date: Fri, 26 Aug 2011 17:30:12 GMT 
Server: Apache/2.2.17 (Unix) DAV/2 Content-Length: 144 
X-Powered-By: Servlet/2.5 JSP/2.1 Cache-Control: no-store, no-cache 
Vary: Accept-Encoding,User-Agent Pragma: no-cache 
Content-Type: application/xml 202

With the returned XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<status xmlns="http://jaxb.rest.pageone.com" description="No Devices to add">202</status>

As far as I can tell, I'm definitely hitting the server, but it doesn't seem to receive the data I'm sending it..

Stumped. Hope someone can point me in the right direction!

Cheers,


Looks like your hitting the server, and likely the data is going to... I think the answer lies in 202:No Devices to add... which the REST interface documentation should explain (perhaps you're missing a required field?) {202 FYI means accepted but no processing was completed, could also mean the user exists}

By the way you should be escaping those arguments you're putting into the POST payload ($fname,$sname,$pass,$phonenumber)... otherwise a weird value (say name) could cause the post to act completely differently to the way you expected. You can do that using urlencode, or by instead building the POST string with http_build_query

<?php
$fields_string=http_build_query(array(
   "firstName"=>$fname,
   "secondName"=>$sname,
   "password"=>$pass,
   "deviceAddress"=>$phonenumber,
   "notes"=>"testing"));
//...
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜