PHP Classes

File: demo5.php

Recommend this page to a friend!
  Classes of Grigori Kochanov   gCurl   demo5.php   Download  
File: demo5.php
Role: Example script
Content type: text/plain
Description: POST request
Class: gCurl
Perform HTTP requests using Curl
Author: By
Last change: Accessible without user login
Date: 16 years ago
Size: 1,049 bytes
 

Contents

Class file image Download
<?php
/**
 * Make a login POST request and receive a session cookie
 *
 */
//Include library
require_once('gCurl/gcurl.class.php');

//init variables
$login = 'mylogin';
$password = 'mypass';
$url = 'http://www.phpclasses.org/login.html?page=';

try{
   
//initialize the class
   
$curl = new gCurl($url,'POST');

   
//prepare POST data
   
$curl->Request->addPostVar('alias',$login);
   
$curl->Request->addPostVar('password',$password);
   
$curl->Request->addPostVar('dologin','1');
   
   
//execute the HTTP request
   
$response = $curl->exec();
}catch (
gksException $E){
   
$message = $E->getLogMessage();
   
file_put_contents('gcurl.error_log.txt',$message);
    echo
$E->getHtmlMessage();
    exit;
}

// if login is correct - the server sets a cookie with a session ID
if ($response->cookies && $response->cookies[0]['name']=='PHPClassesSession'){
    echo
'Login successfull! Session ID: ',$response->cookies[0]['value'];
}else{
    echo
'Invalid login/password or the login form changed';
}