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hii71g2detHRNtHPGKddbEjblrvhiAyws8GPfU+Nr1eWpLkeMh0rd2Io8vjid+WMyRfd+MS56Y35qoS7u0TPSnt/jX8Urf7RlI/UoovRk/zHIgf7cnZdinck2kfQpZvXsxdwPF78eUvvyXI2/8vxxpHW5/WWZcy9UO7vC470Mnd39sVrjUZXLApjaOUAebVNg6VpahggGnS9Xvz0XTf8GUimeoPolnApD/mhlq8YCYYQP3ipziOiQ9h0PkqG2YjdEXkubVlcwD0TJuGxR3y47vBZI75P2e6DPEFFVK18oQYQb14Ocd0ZupTwX4RW03H38rWh5/dE//EMSAqmzybmat+JhVhDCjFJ6LwgSWyBApj3zd/xD96W39bcxSlHmMKflJHWq56DzA3EwVCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCARPi/8Anahbny7pIMgAAAAASUVORK5CYII=



It is possible to send POST data with file_get_contents() and get the returned content, by using it with the stream_context_create() function.
The stream_context_create($opt) creates and returns a stream context with any options supplied in the $opt array.
 

- $opt is an array of associative arrays in the format: $arr['wrapper']['option'] = $value .

Example stream_context_create() and file_get_contents()

$postdata = http_build_query(['name'=>'some name', 'pass'=>'password']);  //data to send 'name=some+name&pass=password'
$opts = [
 'http' =>
  [
   'method'=> 'POST',  // GET or POST
   'header' => 'Content-type: application/x-www-form-urlencoded'. PHP_EOL .'Content-Length: '. strlen($postdata). PHP_EOL,
   'content' => $postdata
  ]
];
$send_data  = stream_context_create($opts);
$cnt = file_get_contents('http://domain.net/page.php', false, $send_data);  //send data and get the returned content

echo $cnt;

 

- The http_build_query($arr) function generates a URL-encoded query string from the $arr, returns a string like this: 'key1=val1&key2=val2'.
 

- The value of the key 'method' must be in UPPERCASE (GET or POST).
 

- The 'header' key contains http headers that must be send to "page.php".

Start typing and press Enter to search