How can I send data to a specific port and ip using Yii2?

247 views Asked by At

How can I send data to a specific port and ip using yii framework? I want send an string to specific ip.

1

There are 1 answers

0
XzAeRo On

Using php you can send data using GET or POST or whatever method you like using this method:

<?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);