search

Home  >  Q&A  >  body text

How to use PHP to transmit data to remote via http url. Thanks.

Now there is a project where Party A has given an http interface as follows

1. Provide interface http access url, uniform character encoding: UTF-8.

2. http interface request content-Type="application/json".

3. The interface requests the interface url through http post and sends a body that conforms to the JSON specification.

The access key is also given, how to implement it using PHP. Thanks.

Request example:

{

"secretKey": "**********",

"stationNo": "123456789",

"msgId": "1111",

"data": {

"heatMeter": [

"{

           "installAddr": "1# Heat Meter",

            "readTime": ""2017-09-25 12:00:01",

                 "accHeat": 11.12,

"accCold": 11.13,

"accFlow": 11.14,

"accCold": 11.13,

"accFlow" ": 11.16,

"inTemp": 11.17,

"outTemp": 21.18,

"tempDiff": 10.01,

"workTime" : 12,

"Workstatus": 0,

"Heatunit": "GJ",

## "" Coldunit ":" gj ",

" Powerunit ":" gj/h " ,

"flowUnit": "m³", "realFlowUnit": "m³/h"

        }

],


Return sample:

{

"msg": "Upload successful" //Here are the details of Chinese characters Problem Description

"msgId":"1111"

"status": 0,

}

odyssey****odyssey****2549 days ago1358

reply all(3)I'll reply

  • NULL

    NULL2017-12-07 13:37:16

    //参数一为提交的地址,参数二为提交方式,参数三为提交数据
    function doRequest($url,$method,$data=null){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        if(!empty($data)){
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
        }
    
        curl_close($ch);
        return $tmpInfo;
    }


    reply
    1
  • 辉

    2017-12-03 12:40:36

    You can also use ajax request

    reply
    0
  • Bamboo

    Bamboo2017-12-01 18:21:02

    Use curl

    reply
    0
  • Cancelreply