php Post请求

在php调用一个 Post请求接口

复制代码
/**
 * PHP发送Json对象数据
 *
 * @param $url 请求url
 * @param $jsonStr 发送的json字符串   charset=utf-8
 * @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)
        )
    );
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
 
    return array($httpCode, $response);
}

/**
 * PHP发送 Post请求
 *
 * @param $url 请求url
 * @param $jsonStr  
 * @return array
 */
function http_post($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     
    $response = curl_exec($ch);
 
    curl_close($ch);
 
    return array($httpCode, $response);
}
复制代码

调用方法

一、JSON方式请求 

复制代码
    //请求 接口  --------------------------------------------------------------------
    $url = "http://111.110.196.159:11585/cdrl/api/mobile/ABCHouse.act";
    //$jsonStr = json_encode(array('tel' => '13478956906', 'companyNo' => '040000'));
    $jsonStr = json_encode(array('tel' => $Tel, 'companyNo' => $CompanyNo ));
    list($returnCode, $returnContent) = http_post_json($url, $jsonStr);
    WriteLog("$LogParm 请求接口 返回码:".$returnCode."  参数:" .$jsonStr."  返回值:" .$returnContent );

    //请求失败---- 返回码 不为200 则 认为 请求失败----------
    if($returnCode!="200"){    
        $arr_result['action']='Action_Hangup';
        WriteLog("$LogParm ". "请求接口失败 " . $returnCode ." $url   $jsonStr " );
        WriteLog("$LogParm 返回值 " .json_encode($arr_result));
        echo json_encode($arr_result );
        exit(0);
    }

    //分析 returnContent
    $obj_returnContent= json_decode($returnContent);//JSON转为object对象
    if($obj_returnContent->message=="find the data"){
    $arr_data = object_array($obj_returnContent->data);//JSON转为数组  
        $data_count = count($arr_data);//数组的长度
        //WriteLog("是否  returnContent 为 ". $arr_data[0]['address'] ." " .$data_count ." " );

     for($i=0;$i<$data_count;$i++){
       $num=$i+1;
       $play_content=$play_content .''.$num ."". $arr_data[$i]['address'];
     }

   }
复制代码

二、Post方式请求

   $url = "http://111.111.24.15/lyyl/callcenter/api/external/getAbbbbt.act";  
   $jsonStr = array('callid' => $Tel );
list($returnCode, $returnContent)
= http_post($url, json_encode($jsonStr));
WriteLog(
"$LogParm 请求 接口 返回码:".$returnCode." 返回值:" .$returnContent." 参数:" .json_encode($jsonStr) );

 

posted @   海乐学习  阅读(308)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2022-11-24 vue 读取 外部配置文件
2015-11-24 JQuery EasyUI combobox 省市两级联动
2015-11-24 JQuery EasyUI combobox动态添加option
2015-11-24 php截取字符去掉最后一个字符
点击右上角即可分享
微信分享提示