当前位置 主页 > 关于我们 > 站长日志 >

    curl_exec,PHP中的curl_exec获取返回值中的json

    栏目:站长日志 时间:2021-03-26 17:09

         1.初始化一个curl对象
    $curl = curl_init();
         2.设置
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 
     
    $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, 
     
    $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);//0,错误时不要失败
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //获取的信息以文件流的形式返回,而不是直接输出curl_setopt($curl, CURLOPT_HEADER, false); //返回头部信息,设置为false,结果中就没有头部信息
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, 
     
    CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
    $data = curl_exec($curl);
    return $data;
         返回值:
     
    {"code":0,"data":{"city":{"cityId":284609,"counname":"\u4e2d
     
    \u56fd","ianatimezone":"Asia\/Shanghai","name":"\u4e1c\u57ce
     
    \u533a","pname":"\u5317\u4eac\u5e02","secondaryname":"\u5317\u4eac
     
    \u5e02","timezone":"8"},"hourly":[{"condition":"\u6674","conditionId":"1","date":"2019
     
    -07-
     
    03","hour":"8","humidity":"54","iconDay":"0","iconNight":"30","pop":"0","pressure":"100
     
    2","qpf":"0.0","realFeel":"27","snow":"0","temp":"25","updatetime":"2019-07-03 
     
    08:04:20","uvi":"4","windDegrees":"45","windDir":"NNE","windSpeed":"4","windlevel":"1"}
     
    ,{"condition":"\u6674","conditionId":"1","date":"2019-07-
     
    03","hour":"9","humidity":"48","iconDay":"0","iconNight":"30","pop":"0","pressure":"100
     
    2","qpf":"0.0","realFeel":"29","snow":"0","temp":"28","updatetime":"2019-07-03 
     
    08:04:20","uvi":"6","windDegrees":"45","windDir":"NNE","windSpeed":"3","windlevel":"1"}
     
    ]},,"msg":"success","rc":{"c":0,"p":"success"}}
     
         原文链接:https://blog.csdn.net/hechenhongbo/article/details/94546262
    下一篇:没有了