php 百度api请求代码 如何写请求

php 百度api 请求代码 如何请求

请求示例:https://dev2.baidu.com/content?sceneType=0&pageId=101141&nodeId=649&subhead=%E8%AF%B7%E6%B1%82%E7%A4%BA%E4%BE%8B

请求地址
https://api.baidu.com/json/sms/service/LeadsNoticeService/getNoticeList
请求header

 "header"
{
    "username": "3B19HY00230",
    "password": "82bnn1",
    "token": "31d351d1d34d0a0f"
}

请求body

{
    "solutionType": "form",
    "startDate": "2021-12-01",
    "endDate": "2021-12-31",
    "pageNo": 1,
    "pageSize": 2
}



img

代码如下,不过题主给的数据包视乎不对。。自己看下api需要发送什么参数

img

<?php
/**
 * PHP发送Json对象数据
 *
 * @param $url 请求url
 * @param $jsonStr 发送的json字符串
 * @return array
 */
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr),
            "username: 3B19HY00230",
            "password: 82bnn1",
            "token: 31d351d1d34d0a0f"
 
        )
    );
    //不验证证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);


    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
 
    return array($httpCode, $response);
}
 
$url = "https://api.baidu.com/json/sms/service/LeadsNoticeService/getNoticeList";
$jsonStr = json_encode(array(
    "solutionType"=>"form",
    "startDate"=> "2021-12-01",
    "endDate"=> "2021-12-31",
    "pageNo"=> 1,
    "pageSize"=> 2
 ));

 list($returnCode, $returnContent) = http_post_json($url, $jsonStr);
 echo $returnContent;
?>

img


有其他问题可以继续交流~


 "username: 3B19HY00230",
            "password: 82bnn1",
            "token: 31d351d1d34d0a0f"
以下key错误,但是我按以上代码保存,写入正确的key依然如上面的测试报错。