简体   繁体   English

cURL没有发布查询字符串

[英]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; 我对cURL的经验不足,我花了几天的时间来解决这个问题:我遇到了cURL的问题,当我提交POST请求时,cURL没有将查询字符串附加到标题中的URL上; 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. 我认为POST应该以完整的域名开头,但我不确定。 If I'm posting data, shouldn't Content-Length be '0' instead of what I am getting? 如果我要发布数据,则Content-Length不应为“ 0”,而不是我得到的是什么?

The outgoing header looks like this: 传出标头如下所示:

POST /rest/v1/oliver/groups/ORIGINNUMBER/member? HTTP/1.1
Accept: application/xml
Content-type: text/plain
User-Agent: Custom PHP Script
Host: campaign.oventus.com
Cookie:  JSESSIONID=SECRETCOOKIE
Content-Length: 95

My php code looks like this: 我的php代码如下所示:

$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:

<?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} 看起来像是您在服务器上,并且可能是数据将要到达...我认为答案在于202:No Devices to add ... REST接口文档应解释(也许您缺少必填字段?) {202 FYI表示已接受,但未完成任何处理,也可能意味着该用户存在}

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. 顺便说一句,您应该转义要放入POST负载中的那些参数( $fname$sname$pass$phonenumber )...否则,怪异的值(例如name)可能导致帖子的行为与您期望的方式。 You can do that using urlencode , or by instead building the POST string with http_build_query 您可以使用urlencode来实现,也可以使用http_build_query构建POST字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM