如何在PHP中使用CURL复制POST请求

时间:2015-04-03 14:28:51

标签: php post curl

我有一个webapp,需要能够重新创建供应商提供的应用程序的后期操作。该应用程序允许用户登录或退出电话工作组。我已经捕获了应用程序发送的HTTP Post请求,以启动会话并将用户登录进出工作组。我想使用CURL在PHP中重新创建这些POST,但是我遇到了一些使POST正确的问题。

我试图模仿从WireShark捕获的帖子如下所示:

POST /Login?timeout=6 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 10.1.##.##:5447
Content-Length: 160
Expect: 100-continue
Connection: Keep-Alive

{"username":"joell","user-auth-token":"TOKENTOKENTOKEN","user-role":"admin_role","client-type":3,"app-id":"cmwin.18.62.7800.0"}

回复

HTTP/1.1 200 OK
Content-Length: 58
Content-Type: text/plain; charset=UTF-8
Connection: Keep-Alive
Cache-Control: no-store
Date: Fri, 03 Apr 2015 13:08:42 GMT
Expires: Fri, 03 Apr 2015 13:08:42 GMT
Access-Control-Allow-Origin: *
Set-Cookie: SessionId=2006727099

我试图重新创建这个的PHP代码是:

$data = array(
            "username" => "joell",
            "user-auth-token" => "TOKENTOKENTOKEN",
            "user-role" => "admin_role",
            "client-type" => 3,
            "app-id" => "cmwin.18.62.7800.0"
            );                                                                    
    $data_string = json_encode($data);                                                                                   

    $curl = curl_init('http://10.1.##.##:5447/Login?timeout=6');                                                                      
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/x-www-form-urlencoded',                                                                                
        'Content-Length: ' . strlen($data_string),
        'Expect: 100-continue',
        'Connection: Keep-Alive')                                                                       
    );   

    if(!curl_exec($curl)){
        die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
    }
    $result = curl_exec($curl);
    print_r($result);
    curl_close($curl);

我的脚本生成的请求是:

POST /Login?timeout=6 HTTP/1.1
Host: 10.1.##.##:5447
Accept: */*
Content-Type: application/x-www-form-urlencoded
Content-Length: 160
Expect: 100-continue
Connection: Keep-Alive

{"username":"joell","user-auth-token":"TOKETOKETOKEN","user-role":"admin_role","client-type":3,"app-id":"cmwin.18.62.7800.0"}

回复

HTTP/1.1 200 OK
Content-Length: 20
Content-Type: text/plain; charset=UTF-8
Connection: Keep-Alive
Cache-Control: no-store
Date: Fri, 03 Apr 2015 15:15:44 GMT
Expires: Fri, 03 Apr 2015 15:15:44 GMT
Access-Control-Allow-Origin: *

目前我的PHP脚本的输出是:

{"error":2147483650}

0 个答案:

没有答案