PHP-Curl模擬HTTP請求

使用PHP-Curl方式模擬HTTP請求,測試接口傳參和返回值狀態php

<?php

/**
 * 模擬post進行url請求
 * @param string $url
 * @param array $postData
 */
function request_post($url = '', $postData = array()) {
    if (empty($url) || empty($postData)) {
        return false;
    }

    $postUrl = $url;
    $ch = curl_init();//初始化curl
    //轉義
    $vars = http_build_query($postData, '', '&');

    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定網頁
    curl_setopt($ch, CURLOPT_HEADER, 0);//設置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果爲字符串且輸出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//參數
    $data = curl_exec($ch);//運行curl
    curl_close($ch);

    return $data;
}

/**
 * 測試
 * @param string $url
 */
function testAction() {
    $url = 'http://www.testing2.ifchange.com/atsng/atsInternal/insertRecruitMessage';
    $postData['app_id'] = 5;
    $postData['uid'] = 1226;
    $postData['user_id'] = 1226;
    $postData['tob_resume_id'] = 0;
    $postData['tob_position_id'] = 0;
    $postData['type'] = 14;
    $postData['content'] = array('');
    $res = request_post($url, $postData);
    print_r($res);

}

testAction();

結果:bash

{"err_msg":"","err_no":0,"results":{"uid":"1226","type":"14","tob_position_id":"0","tob_resume_id":"0","content":"
[null]","status":0,"updated_at":"2018-05-03 15:46:32","created_at":"2018-05-03 15:46:32","message_id":3306}}

這個請求和Postman請求是等效的,以下:app

相關文章
相關標籤/搜索