“Demande de post PHP” Réponses codées

PHP Post

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // do logic
  $name = $_POST['fname'];
}
?>
Andrew Lautenbach

Demande de post PHP

$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);
mountainpython

Poster PHP

<?php
echo 'Hello ' . htmlspecialchars($_POST["name"]) . '!';
?>
Tame Tuatara

PHP Envoyer la demande de poste

// CURL-less method with PHP5
$context  = stream_context_create(array(
  	// use key 'http' even if you send the request to https
    'http' => array(
      	//'header' and 'content' is optional
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query(
          array('key1' => 'value1', 'key2' => 'value2')
        )
    )
));
$result = file_get_contents('http://server.com/path', false, $context);
if ($result === FALSE) { 
	echo "an error occurred during post.";
    http_response_code(500);
} else {
  	// success
	var_dump($result);
}
try { hard = Coder(); }

Réponses similaires à “Demande de post PHP”

Questions similaires à “Demande de post PHP”

Plus de réponses similaires à “Demande de post PHP” dans PHP

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code