Problem using CURL

Status
Not open for further replies.
10 comments
try this:
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect
CURLOPT_TIMEOUT => 30, // timeout on response
CURLOPT_MAXREDIRS => 5, // stop after 10 redirects
CURLOPT_COOKIEFILE => dirname(__FILE__) . "/cookies.txt",
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;

return $header;
}
 
guys whenever i try submitting the form on ipower.com the curl fails to connect , i mean it doesnt connects to ipower.com but instead connects to my localhost.

Here are the headers i get :

Code:
http://127.0.0.1/curl2.php

GET /curl2.php HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

HTTP/1.1 200 OK
Date: Sun, 01 Aug 2010 11:10:25 GMT
Server: Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0
X-Powered-By: PHP/5.3.0
Content-Length: 0
Keep-Alive: timeout=5, max=98
Connection: Keep-Alive
Content-Type: text/html
----------------------------------------------------------
http://127.0.0.1/favicon.ico

GET /favicon.ico HTTP/1.1
Host: 127.0.0.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

HTTP/1.1 404 Not Found
Date: Sun, 01 Aug 2010 11:10:28 GMT
Server: Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0
Vary: accept-language,accept-charset
Accept-Ranges: bytes
Keep-Alive: timeout=5, max=97
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=iso-8859-1
Content-Language: en
 
i6620_screenshot-01aug10-07-14.png




PHP:
<?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.ipower.com/secure/login.bml?err=");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$things = array(
 'credential_0' => 'desi',
 'credential_1' => 'mypass'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $things);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');

$output = curl_exec($ch);
curl_close($ch);
?>
I was able to store the cookies to a text file...make sure cookies.txt is writable..
 
Thank you all at last i got it working.
Thanks to t3odor for his function. the full code is as follows in case any1 needs it :d
Had to edit some parts though but you got my mind working =)
Code:
<?php
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect
CURLOPT_TIMEOUT => 30, // timeout on response
CURLOPT_MAXREDIRS => 5, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEJAR => dirname(__FILE__) . "\cookie1.txt",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "MY POST DATA HERE FOR LOGIN :)"
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;

return $header;
}  //end of fn





function mailbox( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 60, // timeout on connect
CURLOPT_TIMEOUT => 30, // timeout on response
CURLOPT_MAXREDIRS => 5, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEFILE => dirname(__FILE__) . "\cookie1.txt",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => "POST DATA FOR CREATING NEW MAIL BOX HERE"
);

$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );

$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;

return $header;
}  //end of fn

get_web_page("https://secure.ipower.com/secureLogin");
mailbox("http://members.ipower.com/webControl/emailpack/mailcentral.bml");
?>
Thanks again :)
 
Status
Not open for further replies.
Back
Top