如何在curl中执行下面的api

时间:2017-01-04 03:56:02

标签: php api curl

以下是拦截app的流量时获得的API。但我很困惑,因为它的POST HTTP没有帖子文件所以请告诉我如何使用curl运行它

POST /api/Question/GetQuestionsDetails?user_id=1391&last_attemped_question=586&next_question=468&score=999990900 HTTP/1.1
User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; CENTURION 3 Build/KOT49H)
Host: qzaap.com
Connection: Keep-Alive
Accept-Encoding: gzip
Content-Type: application/x-www-form-urlencoded
Content-Length: 0

1 个答案:

答案 0 :(得分:0)

您可以使用此方法:

function curlPost($host, $params) {
    $header = array(
    "Content-Type: application/x-www-form-urlencoded",
    "User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; CENTURION 3 Build/KOT49H)",    
    "Connection: Keep-Alive",
    "Accept-Encoding: gzip",
    "Content-length: 0",
    );

    $postData = "";
    foreach($params as $key => $value)
    {
        $postData .= $key . '='.urlencode($value).'&';
    }
    $postData = rtrim($postData, '&');

    $curl = curl_init($host);
    curl_setopt($curl, CURLOPT_HEADER, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_HEADER, $header);
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { 
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);  
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
    $response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ($status != 200) {
        throw new Exception("Error: call to URL $host failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl) . "\n");
    }
    return $response; 
}

//With your example
$host = "http://qzaap.com/api/Question/GetQuestionsDetails";
$params = array(
    "user_id" => 1391,
    "last_attemped_question" => 586,
    "next_question" => 468,
    "score" => 999990900
);

echo curlPost($host, $params);