Created
July 5, 2011 03:07
-
-
Save ricardosdl/1064196 to your computer and use it in GitHub Desktop.
Código php responsável por enviar uma requisição do tipo POST
This file contains hidden or 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
<?php | |
//url do destino da requsiçao, equivalente ao "action" de um formulário | |
$url = 'http://localhost/www/teste_post.php'; | |
//estes seriam os "inputs" do formulário | |
$campos = array( | |
'nome'=>urlencode("Ricardo"), | |
'idade'=>urlencode("4321"), | |
'email'=>urlencode("ricardo@gmail.com"), | |
'telefone'=>urlencode("666-999") | |
); | |
//temos que colocar os parâmetros do post no estilo de uma query string | |
foreach($campos as $name => $valor) { | |
$string_campos .= $name . '=' . $valor . '&'; | |
} | |
$string_campos = rtrim($string_campos,'&'); | |
$ch = curl_init(); | |
//configurando as opções da conexão curl | |
curl_setopt($ch,CURLOPT_URL,$url); | |
//este parâmetro diz que queremos resgatar o retorno da requisição | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE); | |
curl_setopt($ch,CURLOPT_POST,count($campos)); | |
curl_setopt($ch,CURLOPT_POSTFIELDS,$string_campos); | |
//manda a requisição post | |
$resultado = curl_exec($ch); | |
curl_close($ch); | |
echo $resultado; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment