2011-08-26 71 views
0

我不是那種cURL的經驗,我已經花了幾天的時間來嘗試排序這個問題:我有一個問題,cURL沒有追加到我的標題中的查詢字符串,當我提交POST請求;因此服務器不會收到「有效負載」,並且我正在訪問的服務返回錯誤狀態碼,表示它沒有收到相應的數據。cURL沒有發佈查詢字符串

我認爲POST應該從完整的域名開始,但我不確定。如果我發佈數據,不應將Content-Length設置爲「0」而不是我所獲得的內容?

輸出頭看起來是這樣的:

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 

我的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); 

和服務器返回的標題是這樣的:

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 

隨着返回的XML:

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

至於我可以告訴大家,我肯定擊中服務器,但它似乎並沒有收到我送它的數據..

難住了。希望有人能指引我正確的方向!

乾杯,

+0

一切我是做了罰款,但REST服務已不能正常運行。 Bug修復了他們的結局,這一切都很好。 GRR。 – oliver

回答

1

看起來你打的服務器,可能數據會......我想,答案就在​​...其中REST接口文檔應該說明(也許你錯過了一個必填字段?){202 FYI意味着接受,但完成任何處理,也可能意味着該用戶存在}

通過你應該逃避那些你投入的POST有效載荷($fname$sname參數的方式,$pass$phonenumber)...否則一個奇怪的值(比如名字)可能會導致帖子完全不同於你的預期特德。你可以做,使用urlencode,或不是建立在POST字符串http_build_query

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

感謝您的回覆。不幸的是,文檔沒有詳細介紹足夠的細節。但是你確定發佈的數據實際上正在通過? – oliver

+0

嗯,不知道更多關於系統和文檔的知識,但沒有理由不把數據發佈到遠程服務器(不管它是否把它扔掉是另一回事)。最好的解決方法是創建一個本地'test.php',將POST的內容簡單地記錄到一個文件中('<?php file_put_contents(「post.log」,print_r($ _ POST,true)); exit();? >'然後將你的URL改爲本地頁面,看看日誌中出現了什麼 – Rudu

+0

原來這實際上是我使用的REST服務的一個問題 - 它錯過了我的請求。 – oliver