Jump to content

using curl to post to https


daydreamer

Recommended Posts

hi.

 

I am trying to use curl to post to a https page.

 

in specific, i am trying to change a password.

 

<?php 
$pw = 'smudge1';
$newpass = 'itworked';

//Go to password change page (after main page has logged in.)
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookiefile777');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookiefile777'); # SAME cookiefile
curl_setopt($curl, CURLOPT_POSTFIELDS, "in_oldPassword=$pw&in_newPassword=$newpass&Confirm_Password=$newpass&submit=Save");
curl_setopt($curl, CURLOPT_URL, 'https://registration.o2.co.uk/Registration/Update');
$xxx = curl_exec($curl);
echo $xxx;

curl_close ($curl);
?>

 

The result is a page from the 02 server saying "Unfortunately a problem was encountered while trying to process your request.".

 

Normally if you have entered any incorrect details this error would tell you what is wrong.

 

Do i have to use curl_setopt to set something which enables https?

 

Does anyone have any suggestions, questions, hints, tips, anything will help!

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/105799-using-curl-to-post-to-https/
Share on other sites

yep i have, another script does that part. This script can defiantly get to the page where you enter the details of new password, old password etc, I have tested by echoing the result before the post attempt.

 

The page also lets you change other details like name, address etc, but I want to keep these the same so have only included these details:

 

curl_setopt($curl, CURLOPT_POSTFIELDS, "in_oldPassword=$pw&in_newPassword=$newpass&Confirm_Password=$newpass&submit=Save");

 

Should I be including the other parts as well (i have tried this tho, same error).

 

Thanks for any help - if you need any other info please ask!

I just used cURL for the first time the other day and I found a nice class that made things easier than I thought they would be.

 

Here's the class and the docs http://github.com/shuber/curl/tree/master

<?php

# Curl, CurlResponse
#
# Author  Sean Huber - [email protected]
# Date    May 2008
#
# A basic CURL wrapper for PHP
#
# See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP

class Curl {

public $cookie_file = 'curl_cookie.txt';
public $headers = array();
public $options = array();
public $referer = '';
public $user_agent = '';

protected $error = '';
protected $handle;

public function __construct() {
	$this->user_agent = $_SERVER['HTTP_USER_AGENT'];
}

public function delete($url, $vars = array()) {
	return $this->request('DELETE', $url, $vars);
}

public function error() {
	return $this->error;
}

public function get($url, $vars = array()) {
	if (!empty($vars)) {
		$url .= (stripos($url, '?') !== false) ? '&' : '?';
		$url .= http_build_query($vars);
	}
	return $this->request('GET', $url);
}

public function post($url, $vars = array()) {
	return $this->request('POST', $url, $vars);
}

public function put($url, $vars = array()) {
	return $this->request('PUT', $url, $vars);
}

protected function request($method, $url, $vars = array()) {
	$this->handle = curl_init();

	# Set some default CURL options
	curl_setopt($this->handle, CURLOPT_COOKIEFILE, $this->cookie_file);
	curl_setopt($this->handle, CURLOPT_COOKIEJAR, $this->cookie_file);
	curl_setopt($this->handle, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($this->handle, CURLOPT_HEADER, true);
	curl_setopt($this->handle, CURLOPT_POSTFIELDS, http_build_query($vars));
	curl_setopt($this->handle, CURLOPT_REFERER, $this->referer);
	curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($this->handle, CURLOPT_URL, $url);
	curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);

	# Format custom headers for this request and set CURL option
	$headers = array();
	foreach ($this->headers as $key => $value) {
		$headers[] = $key.': '.$value;
	}
	curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers);

	# Determine the request method and set the correct CURL option
	switch ($method) {
		case 'GET':
			curl_setopt($this->handle, CURLOPT_HTTPGET, true);
			break;
		case 'POST':
			curl_setopt($this->handle, CURLOPT_POST, true);
			break;
		default:
			curl_setopt($this->handle, CURLOPT_CUSTOMREQUEST, $method);
	}

	# Set any custom CURL options
	foreach ($this->options as $option => $value) {
		curl_setopt($this->handle, constant('CURLOPT_'.str_replace('CURLOPT_', '', strtoupper($option))), $value);
	}

	$response = curl_exec($this->handle);
	if ($response) {
		$response = new CurlResponse($response);
	} else {
		$this->error = curl_errno($this->handle).' - '.curl_error($this->handle);
	}
	curl_close($this->handle);
	return $response;
}

}

class CurlResponse {

public $body = '';
public $headers = array();

public function __construct($response) {
	# Extract headers from response
	$pattern = '#HTTP/\d\.\d.*?$.*?\r\n\r\n#ims';
	preg_match_all($pattern, $response, $matches);
	$headers = split("\r\n", str_replace("\r\n\r\n", '', array_pop($matches[0])));

	# Extract the version and status from the first header
	$version_and_status = array_shift($headers);
	preg_match('#HTTP/(\d\.\d)\s(\d\d\d)\s(.*)#', $version_and_status, $matches);
	$this->headers['Http-Version'] = $matches[1];
	$this->headers['Status-Code'] = $matches[2];
	$this->headers['Status'] = $matches[2].' '.$matches[3];

	# Convert headers into an associative array
	foreach ($headers as $header) {
		preg_match('#(.*?)\:\s(.*)#', $header, $matches);
		$this->headers[$matches[1]] = $matches[2];
	}

	# Remove the headers from the response body
	$this->body = preg_replace($pattern, '', $response);
}

public function __toString() {
	return $this->body;
}

}

?>

Should I be including the other parts as well (i have tried this tho, same error).

 

Thanks for any help - if you need any other info please ask!

 

I would assume your missing some details but without an account or even an O2 phone it very hard for me to say.

  • 3 weeks later...
  • 1 month later...

In your login bit to o2 is this not via the https protocol? I am trying to login to o2 (so you have got further then me :-) what i think you need to do is maintain the session from the login for the change of password. You could do this using the ob_start around the login curl_exec bit. Like I said I can't help / test as i've not managed to login lol.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.