如何使用Yii2将数据发送到特定端口和ip?

时间:2015-06-10 15:20:28

标签: php yii2 yii2-advanced-app

如何使用yii框架将数据发送到特定端口和ip? 我想发一个字符串到特定的IP。

1 个答案:

答案 0 :(得分:1)

使用php,您可以使用GET或POST或任何您喜欢的方法使用此方法发送数据:

<?php
// change this to your own values
$ip = '127.0.0.1';
$port = 80;
$location = '';
$url = "http://$ip:$port/$location";

// define the data you want to send
$params = http_build_query(
    array(
        'name1' => $value1,
        'name2' => $value2,
        // ...
    )
);

// set the method to send the data
$opts = array('http' =>
  array(
    'method'  => 'GET', // or POST or PUT or DELETE
    'content' => $params,
    'header' => 'Content-Type: application/x-www-form-urlencoded\r\n'
    )
);

$context = stream_context_create($opts); //build the http context

// send the data and capture the response
$response = file_get_contents($url.$params, false, $context);