Created
October 14, 2015 08:46
-
-
Save floriancourgey/f5ece3562ddf0b937c4a to your computer and use it in GitHub Desktop.
Formation SI - bot de connexion - base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>PhpFiddle Initial Code</title> | |
</head> | |
<body> | |
<?php | |
/* | |
* exemple requete GET | |
* | |
* on definit une variable $c avec laquelle on appelle quelques fonctions | |
* | |
* puis on affiche le tout avec un "echo" | |
*/ | |
$c = curl_init(); // init requete | |
$url = "www.url-qui-ne-marche-pas.com"; | |
curl_setopt($c, CURLOPT_URL, $url); // definition de l'URL a rejoindre | |
/** vous occupez pas de ca **/ | |
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($c, CURLOPT_CONNECTTIMEOUT , 0); // illimite | |
curl_setopt($c, CURLOPT_TIMEOUT, 10); //secondes | |
/** vous pouvez vous occuper de la suite :) **/ | |
$reponseHtml = curl_exec($c); // on execute la requete et on stocke la reponse HTML dans la varianle $reponseHtml | |
echo $reponseHtml; // on affiche ce qu'on a recu | |
// -> Tout simple ! | |
/** | |
* exemple requete POST | |
* | |
* c'est pareil, on ajoute juste les donnes a envoyer sous forme de paires cle-valeur | |
*/ | |
$c = curl_init(); // init requete | |
$url = "www.url-qui-ne-marche-pas.com"; | |
curl_setopt($c, CURLOPT_URL, $url); // definition de l'URL a rejoindre | |
/** CECI A ETE AJOUTE PAR RAPPORT AU GET, regardez bien :) **/ | |
$fields = array( | |
'cle1' => "valeur1", | |
'cle2' => "valeur2", | |
); | |
/** ceci a ete ajoute mais vous en occupez pas haha **/ | |
curl_setopt($c, CURLOPT_POST, count($fields)); | |
curl_setopt($c, CURLOPT_POSTFIELDS, arrayToPostString($fields)); | |
/** vous occupez pas de ca **/ | |
curl_setopt($c, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($c, CURLOPT_CONNECTTIMEOUT , 0); // illimite | |
curl_setopt($c, CURLOPT_TIMEOUT, 10); //secondes | |
/** vous pouvez vous occuper de la suite :) **/ | |
$reponseHtml = curl_exec($c); // on execute la requete et on stocke la reponse HTML dans la varianle $reponseHtml | |
echo $reponseHtml; // on affiche ce qu'on a recu | |
?> | |
<?php | |
// vous occupez pas de ca | |
function arrayToPostString($array){ | |
if(!is_array($array)){ | |
exit("le tableau de cles-valeurs est invalide"); | |
} | |
$fields_string = ""; | |
foreach($array as $key=>$value) { | |
$fields_string .= $key.'='.$value.'&'; | |
} | |
rtrim($fields_string, '&'); | |
return $fields_string; | |
} | |
?> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment