Updated 2017 PHP code to automatically bid/buy an eBay auction item

Updated code to automatically bid on an eBay item, I’m currently using it myself if a loop of searched items.

Most of the changes are the query strings which is a lot more involved now. For testing use one cent items with free shipping.

	// MAIN BIDDING FUNCTION MAIN BIDDING FUNCTION
	// this function performs no checking on the input variables
	// $username  -  the eBay user name
	// $password  -  the eBay user password
	// $item      -  the eBay item number to bid on
	// $bid       -  the bid amount in local currency i.e. 5.67
	// $link      -  itemlink with referral, can leave empty i.e. ''
	function place_bid($username, $password, $item, $bid, $link) {

		$cookies = dirname(__FILE__).'/cookies.txt';
		
		//set success as default false
		$success = false;
		$bid_success = false;

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)');
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($curl, CURLOPT_REFERER, $link);
		curl_setopt($curl, CURLOPT_COOKIEFILE, $cookies);
		curl_setopt($curl, CURLOPT_COOKIEJAR, $cookies);
		
		//query the sign-out page
		//curl_setopt($curl, CURLOPT_URL, "https://signin.ebay.com/ws/eBayISAPI.dll?SignIn&lgout=1");
		//$ret = curl_exec ($curl);
		
		//query the referal link page
		if ($link) {
			curl_setopt($curl, CURLOPT_URL, $link);
			$ret = curl_exec ($curl);
		}
		
		//IMPORTANT		
		//query the sign-in page to set the cookies
		curl_setopt($curl, CURLOPT_URL, 'https://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn&campid=5337161990&customid=7');
		$ret = curl_exec ($curl);
		
		$data = [];
		$numbers = [];
		if (preg_match('%(<form name="SignInForm".*?<\/form>)%im', $ret, $regs)) {

			$dom = new DOMDocument;
			$dom->loadHTML($regs[0]);
			$nodes = $dom->getElementsByTagName('input');
			
			foreach($nodes as $node) {
				$name  = $node->getAttribute('name');
				$value = $node->getAttribute('value');
				//$size  = $node->getAttribute('size');
				$type  = $node->getAttribute('type');
				
				$data[$name] = $value;
				
				if (is_numeric($name)) {
					if ($type == 'text') {
						$data[$name] = $username;
					}
					if ($type == 'password') {
						$data[$name] = $password;
					}
				}
			}
		}
		
		unset($data['']);
		$data_str = http_build_query($data);
		
		//sign-in
		curl_setopt($curl, CURLOPT_URL, "https://signin.ebay.com/ws/eBayISAPI.dll?co_partnerId=2&siteid=0&UsingSSL=1");
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data_str);
		$ret = curl_exec ($curl);
		
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
			if(curl_errno($curl)){
				ebaylog('Curl error: ' . curl_error($curl));
			}
			if (!$ret) {
				$ret = curl_exec ($curl);
				if(curl_errno($curl)){
					ebaylog('Curl error: ' . curl_error($curl));
				}
			}
		}
		
		curl_setopt($curl, CURLOPT_URL, 'https://www.ebay.com/myb/WatchList');
		$ret = curl_exec ($curl);
		
		//test_write($ret);
		
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
			if(curl_errno($curl)){
				ebaylog('Curl error: ' . curl_error($curl));
			}
			if (!$ret) {
				$ret = curl_exec ($curl);
				if(curl_errno($curl)){
					ebaylog('Curl error: ' . curl_error($curl));
				}
			}
		}

		
		
		//if (strpos($ret, '"loggedIn":true') === FALSE) {
		if ((strpos($ret, "userid={$username}") === FALSE) && (strpos($ret, "usr/{$username}") === FALSE)) {
			if (preg_match('%<b class="altTitle">(.*)</b>%', $ret, $regs)) {
				$err = $regs[1];
				ebaylog("\"{$err}\"");
				if (strpos($err, 'The alerts below') === 0) {
					ebaylog("{$item}: 'The alerts below' found, successful");
					//set it to succes
				}
			} else {
				ebaylog("{$item}: Failed signing in");
				if (preg_match('%<font face=".*?" size="3"><b>(.*?)</b></font>%', $ret, $regs)) {
					$err = $regs[1];
					ebaylog("\"{$err}\"");
				}
				
				//test_write($ret);
				goto end;
			}
			
			
		} else {
			ebaylog("{$item}: Success signing in");
		}
		
		
		//place the inital bid
		curl_setopt($curl, CURLOPT_URL, "https://offer.ebay.com/ws/eBayISAPI.dll?MakeBid&item={$item}&maxbid={$bid}&campid=5337161990&customid=7");
		$ret = curl_exec ($curl);
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
		}

		if (preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="stok"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs)) {
			$stok = $regs[1][0];
		} else {
			//Failed to get 'stok' value
			//try and determine why
			if (preg_match('%<div class="statusDiv">(.*?)</div>%', $ret, $regs)) {
				$err = $regs[1];
				ebaylog("'{$err}'");
				//if string starts with "Enter US $0.41 or more"
				if (stripos($err, 'Enter') === 0) {
					ebaylog("{$item}: 'Enter' found, aborting");
					//set it to success
					$success = true;
				} else if (stripos($err, 'To enter a') === 0) {
					ebaylog("{$item}: 'To enter a' found, aborting");
					//set it to success
					$success = true;
				} else if (stripos($err, 'Transaction Blocked') === 0) {
					ebaylog("{$item}: 'Transaction Blocked' found, aborting");
					//set it to success
					$success = true;
				}
				
			} else if (preg_match('%"\d*" - Invalid Item</div>%', $subject)) {
				ebaylog("{$item}: 'Invalid Item' found, aborting");
				test_write($ret);
				//set it to success
				$success = true;
			} else if (preg_match('%<div class="subTlt"><ul class="errList"><li>(.*?)</li></ul></div>%', $subject)) {
				ebaylog("{$item}: 'no longer available' found, aborting");
				test_write($ret);
				//set it to success
				$success = true;
			} else if (preg_match('%id="w\d-\d-_msg".*?>(.*?)</span>%', $ret, $regs)) {
				ebaylog("'{$regs[1]}'");
			} else if (preg_match('%<div\s+class\s*=\s*"(?:errRed|errBlk|errTitle|statusDiv)"\s*>(.*?)</div>%i', $ret, $regs)) {
				ebaylog("'{$regs[1]}'");
			} else {
				//don't know why so log the page
				ebaylog("{$item}: Failed to get 'stok' value");
				test_write($ret);
			}
			goto end;
		}
		
		if (preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="uiid"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs)) {
			$uiid = $regs[1][0];
		} else {
			ebaylog("{$item}: Failed to get 'uiid' value");
			goto end;
		}
		

		if ($stok && $uiid) {
			ebaylog("{$item}: Success placing initial bid");
		} else {
			ebaylog("{$item}: Failed placing initial bid");
			goto end;
			
		}
		
		//confirm the bid
		curl_setopt($curl, CURLOPT_URL, "https://offer.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeBid&maxbid={$bid}&quant=1&mode=1&stok={$stok}&uiid={$uiid}&co_partnerid=2&user={$username}&fb=0&item={$item}&campid=5337161990&customid=7");
		$ret = curl_exec ($curl);
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
		}
		
		//perform a number of tests to determine if the bid was a success
		$bid_success = true;
		if (stripos($ret, "you're the first bidder") === FALSE) {
			if (stripos($ret, "you're the high bidder and currently in the lead") === FALSE) {
				if (stripos($ret, "you're currently the highest bidder") === FALSE) {
					$bid_success  = false;
					ebaylog("{$item}: Failed placing final bid");
					//try and determine why
					if (preg_match('%<div\s+class\s*=\s*"(?:errRed|errBlk|errTitle|statusDiv|title)"\s*>(.*?)</div>%i', $ret, $regs)) {
						$err = $regs[1];
						ebaylog("'{$err}'");
						if (stripos($err, 'Enter') === 0) {
							ebaylog("{$item}: 'Enter' found, aborting");
							//set it to success
							$bid_success = true;
						} else if (stripos($err, "You've just been outbid") === 0) {
							ebaylog("{$item}: 'You've just been outbid' found, aborting");
							//set it to success
							$bid_success  = true;
						} else if (stripos($err, "You're currently the high bidder,") === 0) {
							ebaylog("{$item}: 'You're currently the high bidder, but the reserve hasn't been met.' found, aborting");
							//set it to success
							$bid_success  = true;
						}
					} else {
						//we don't know why it failed so write the data
						test_write($ret);
					}
				}
			}
		}
		
		if ($bid_success) {
			ebaylog("{$item}: Success placing final bid");
			$success = true;
		}
		
		end:
		
		//close the curl session
		curl_close ($curl);
		
		if ($success) {
			ebaylog("{$item}: Success: {$username}");
		} else {
			ebaylog("{$item}: Failure: {$username}");
		}
		
		return $success;
	}
	
	//for testing
	function test_write($out) {
		$fh = fopen(dirname(__FILE__).'/'.date('Y-m-d H-i-s').'.html', 'w');
		fwrite($fh, $out);
		fclose($fh);
	}

	//simple logging function
	function ebaylog($msg) {
		$msg = "{$msg}\r\n";
		echo $msg;
		$fh = fopen(dirname(__FILE__).'/ebay-log.txt', 'a');
		fwrite($fh, date('Y-m-d H-i-s')." {$msg}");
		fclose($fh);
	}

CloudSight API PHP example

Here is an example PHP function for the CloudSight API, given there are not many examples online.

Simply input your API key and the url which could be either a local or internet file, the function will return the string description of the image.

	function cloudsightapi($key, $file_url) {
		
		$parse = parse_url($file_url);
		
		$size = getimagesize($file_url);
		$boundary = md5(time());
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_URL, "https://api.cloudsightapi.com/image_requests");
		
		if (isset($parse['host'])) {
			$headers = array('Authorization: CloudSight '.$key, "Content-Type:multipart/form-data");
			
			$postFields = array(
				'image_request' => array(
					'remote_image_url'  => $file_url,
					'locale' => 'en-US'
				)
			);
			
			$body = http_build_query($postFields);
		
		} else {
			$headers = array('Authorization: CloudSight '.$key, "Content-Type:multipart/form-data; boundary=".$boundary);
			
			$body  = '';
			$body .= '--'.$boundary."\r\n";
			$body .= 'Content-Disposition: form-data; name="image_request[locale]"'."\r\n\r\n";
			$body .= "en-US"."\r\n";
			$body .= '--'.$boundary."\r\n";
			$body .= 'Content-Disposition: form-data; name="image_request[image]"; filename="'.basename($file_url).'"'."\r\n";
			$body .= 'Content-Type: '.$size['mime']."\r\n";
			$body .= 'Content-Transfer-Encoding: multipart/form-data'."\r\n\r\n";
			$body .= file_get_contents($file_url)."\r\n";
			$body .= '--'.$boundary .'--'."\r\n\r\n";
			
		}
		
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

		$ret = curl_exec($ch);
		$json = json_decode($ret, true);
		
		$token = $json['token'];
		$status = $json['status'];
		
		while ($status == 'not completed') {
			
			//grab the result
			curl_setopt($ch, CURLOPT_POST, false);
			curl_setopt($ch, CURLOPT_POSTFIELDS, null);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
			curl_setopt($ch, CURLOPT_URL, 'https://api.cloudsightapi.com/image_responses/'.$token);
			$ret = curl_exec($ch);
			
			$json = json_decode($ret, true);
			
			$token = $json['token'];
			$status = $json['status'];
			
			//print_R($json);
			
			sleep(1);
		}
			
		$label = $json['name'];
		
		curl_close($ch);
		
		return $label;
	
	}

Airbnb coupon code new signup $50 AUD with referral

Here is an Airbnb coupon code, get $50 AUD when you take a first trip of $100 AUD or more using out referral link below:

www.airbnb.com/c/shydzik

You can also get $100 AUD when you welcome your first guest as a host.

PHP code/script to automatically purchase a Buy it Now (BIN) eBay item

Similar to my previous eBay bidding script, here is PHP code to automatically purchase a BIN item.

Note the script fails if immediate PayPal payment is required. Payment still needs to be performed manually.

Function takes item number only, so be careful with it.

	// MAIN BIDDING FUNCTION MAIN BIDDING FUNCTION
	// this function performs no checking on the input variables
	// $username  -  the eBay user name
	// $password  -  the eBay user password
	// $item      -  the eBay item number to buy
	// $link      -  itemlink with referral, can leave empty i.e. ''
	function place_bin($username, $password, $item, $link) {

		$cookies = dirname(__FILE__).'/cookies.txt';
		
		//set success as default false
		$success = false;
		$bid_success = false;

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1');
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($curl, CURLOPT_REFERER, $link);
		curl_setopt($curl, CURLOPT_COOKIEFILE, $cookies);
		curl_setopt($curl, CURLOPT_COOKIEJAR, $cookies);
		
		//query the sign-out page
		//curl_setopt($curl, CURLOPT_URL, "http://signin.ebay.com/ws/eBayISAPI.dll?SignIn&lgout=1");
		//$ret = curl_exec ($curl);
		
		//IMPORTANT		
		//query the sign-in page to set the cookies
		curl_setopt($curl, CURLOPT_URL, 'http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn&campid=5337161990&customid=7');
		curl_exec ($curl);
		
		//query the referal link page
		if ($link) {
			curl_setopt($curl, CURLOPT_URL, $link);
			$ret = curl_exec ($curl);
		}
		
		//sign-in
		curl_setopt($curl, CURLOPT_URL, "http://signin.ebay.com/aw-cgi/eBayISAPI.dll?MfcISAPICommand=SignInWelcome&siteid=0&co_partnerId=2&UsingSSL=0&ru=&pp=&pa1=&pa2=&pa3=&i1=-1&pageType=-1&userid={$username}&pass={$password}");
		$ret = curl_exec ($curl);
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
			if(curl_errno($curl)){
				ebaylog('Curl error: ' . curl_error($curl));
			}
			if (!$ret) {
				$ret = curl_exec ($curl);
				if(curl_errno($curl)){
					ebaylog('Curl error: ' . curl_error($curl));
				}
			}
		}
		
		if (strpos($ret, '"loggedIn":true') === FALSE) {
			if (preg_match('%<b class="altTitle">(.*)</b>%', $ret, $regs)) {
				$err = $regs[1];
				ebaylog("\"{$err}\"");
				if (strpos($err, 'The alerts below') === 0) {
					ebaylog("{$item}: 'The alerts below' found, successful");
					//set it to succes
				}
			} else {
				ebaylog("{$item}: Failed signing in");
				if (preg_match('%<font face=".*?" size="3"><b>(.*?)</b></font>%', $ret, $regs)) {
					$err = $regs[1];
					ebaylog("\"{$err}\"");
				}
				
				//test_write($ret);
				goto end;
			}
			
			
		} else {
			ebaylog("{$item}: Success signing in");
		}
		
		
		//place the initial bin
		curl_setopt($curl, CURLOPT_URL, "http://offer.ebay.com/ws/eBayISAPI.dll?BinConfirm&item={$item}&quantity=1&campid=5337161990&customid=7");
		$ret = curl_exec ($curl);
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
		}

		
		if (preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="stok"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs)) {
			$stok = $regs[1][0];
		} else {
			//Failed to get 'stok' value
			//try and determine why
			
			//check if immediate paypal checkout required
			if (preg_match('%<p\W*>\W*(.*)</p>%i', $ret, $regs)) {
				$err = $regs[1];
				if (stripos($ret, "You're almost done!") === 0) {
					ebaylog("Requires immediate PayPal payment.");
					//set it to success
					$success = true;
					goto end;
				} else {
					test_write($ret);
				}
			
			} else if (preg_match('%<div class="statusDiv">(.*?)</div>%', $ret, $regs)) {
				$err = $regs[1];
				ebaylog("'{$err}'");
				//if string starts with "Enter US $0.41 or more"
				if (stripos($err, 'Transaction Blocked') === 0) {
					ebaylog("{$item}: 'Transaction Blocked' found, aborting");
					//set it to success
					$success = true;
				} else {
					test_write($ret);
				}
				
			} else if (preg_match('%"\d*" - Invalid Item</div>%', $ret)) {
				ebaylog("{$item}: 'Invalid Item' found, aborting");
				test_write($ret);
				//set it to success
				$success = true;
			} else if (preg_match('%<div class="subTlt"><ul class="errList"><li>(.*?)</li></ul></div>%', $ret)) {
				ebaylog("{$item}: 'no longer available' found, aborting");
				test_write($ret);
				//set it to success
				$success = true;
			} else if (preg_match('%id="w\d-\d-_msg".*?>(.*?)</span>%', $ret, $regs)) {
				ebaylog("'{$regs[1]}'");
			} else if (preg_match('%<div\s+class\s*=\s*"(?:errRed|errBlk|errTitle|statusDiv)"\s*>(.*?)</div>%i', $ret, $regs)) {
				ebaylog("'{$regs[1]}'");
			} else {
				//don't know why so log the page
				ebaylog("{$item}: Failed to get 'stok' value");
				test_write($ret);
			}
			goto end;
		}
		
		if (preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="uiid"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs)) {
			$uiid = $regs[1][0];
		} else {
			ebaylog("{$item}: Failed to get 'uiid' value");
			goto end;
		}
		

		if ($stok && $uiid) {
			ebaylog("{$item}: Success placing initial bid");
		} else {
			ebaylog("{$item}: Failed placing initial bid");
			goto end;
			
		}
		
		//confirm the bid
		$temp = "http://offer.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=BinConfirm&quantity=1&mode=1&stok={$stok}&xoredirect=true&uiid={$uiid}&co_partnerid=2&user={$username}&fb=0&item={$item}&campid=5337161990&customid=7";
		curl_setopt($curl, CURLOPT_URL, $temp);
		$ret = curl_exec ($curl);
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
		}
		
		if (stripos($ret, 'Commit to buy') !== FALSE) {
			ebaylog('Trying again');
			$ret = curl_exec ($curl);
			if(curl_errno($curl)){
				ebaylog('Curl error: ' . curl_error($curl));
			}
			if (!$ret) {
				$ret = curl_exec ($curl);
			}
		}
		
		//perform a number of tests to determine if the bid was a success
		$bid_success = true;
		if (stripos($ret, 'Please pay now to complete your purchase.') === FALSE) {
			$bid_success  = false;
			ebaylog("{$item}: Failed placing final bid");
			//try and determine why
			if (preg_match('%<div\s+class\s*=\s*"(?:errRed|errBlk|errTitle|statusDiv|title)"\s*>(.*?)</div>%i', $ret, $regs)) {
				$err = $regs[1];
				ebaylog("'{$err}'");
			} else {
				//we don't know why it failed so write the data
				test_write($ret);
				ebaylog($temp);
			}
		}
		
		if ($bid_success) {
			ebaylog("{$item}: Success placing final bid");
			$success = true;
		}
		
		end:
		
		//close the curl session
		curl_close ($curl);
		
		if ($success) {
			ebaylog("{$item}: Success: {$username}");
		} else {
			ebaylog("{$item}: Failure: {$username}");
		}
		
		return $success;
	}

Misc functions below;

	//for testing
	function test_write($out) {
		$fh = fopen(dirname(__FILE__).'/'.date('Y-m-d H-i-s').'.html', 'w');
		fwrite($fh, $out);
		fclose($fh);
	}

	//simple logging function
	function ebaylog($msg) {
		$msg = "{$msg}\r\n";
		echo $msg;
		$fh = fopen(dirname(__FILE__).'/ebay-log.txt', 'a');
		fwrite($fh, date('Y-m-d H-i-s')." {$msg}");
		fclose($fh);
	}

Updated PHP code to automatically bid/buy an eBay auction item

This code is now obsolete and has been updated again.

Updated code to automatically bid on an eBay item, I’m currently using it myself if a loop of searched items.

Most of the changes are improved return status checking. For testing use one cent items with free shipping.

// MAIN BIDDING FUNCTION
// this function performs no checking on the input variables
// $username  -  the eBay user name
// $password  -  the eBay user password
// $item      -  the eBay item number to bid on
// $bid       -  the bid amount in local currency i.e. 5.67
// $link      -  referral link, can leave empty i.e. '' or use 'http://ebay.com'
function place_bid($username, $password, $item, $bid, $link) {

	$cookies = dirname(__FILE__).'/cookies.txt';
	
	//set success as default false
	$success = false;
	$bid_success = false;

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($curl, CURLOPT_REFERER, $link);
	curl_setopt($curl, CURLOPT_COOKIEFILE, $cookies);
	curl_setopt($curl, CURLOPT_COOKIEJAR, $cookies);
	
	//query the referal link page and grab tracking part
	curl_setopt($curl, CURLOPT_URL, $link);
	$ret = curl_exec ($curl);
	
	//query the sign-out page
	//curl_setopt($curl, CURLOPT_URL, "http://signin.ebay.com/ws/eBayISAPI.dll?SignIn&lgout=1");
	//$ret = curl_exec ($curl);
	
	//IMPORTANT		
	//query the sign-in page to set the cookies
	curl_setopt($curl, CURLOPT_URL, 'http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn&campid=5337161990&customid=7');
	curl_exec ($curl);
	
	//sign-in
	curl_setopt($curl, CURLOPT_URL, "http://signin.ebay.com/aw-cgi/eBayISAPI.dll?MfcISAPICommand=SignInWelcome&siteid=0&co_partnerId=2&UsingSSL=0&ru=&pp=&pa1=&pa2=&pa3=&i1=-1&pageType=-1&userid={$username}&pass={$password}&campid=5337161990&customid=7");
	$ret = curl_exec ($curl);
	if(curl_errno($curl)){
		ebaylog('Curl error: ' . curl_error($curl));
	}
	if (!$ret) {
		$ret = curl_exec ($curl);
		if(curl_errno($curl)){
			ebaylog('Curl error: ' . curl_error($curl));
		}
		if (!$ret) {
			$ret = curl_exec ($curl);
			if(curl_errno($curl)){
				ebaylog('Curl error: ' . curl_error($curl));
			}
		}
	}
	
	if (strpos($ret, "Member id {$username}") === FALSE) {
		if (preg_match('%<b class="altTitle">(.*)</b>%', $ret, $regs)) {
			$err = $regs[1];
			ebaylog("\"{$err}\"");
			if (strpos($err, 'The alerts below') === 0) {
				ebaylog("{$item}: 'The alerts below' found, successful");
				//set it to succes
			}
		} else {
			ebaylog("{$item}: Failed signing in");
			if (preg_match('%<font face=".*?" size="3"><b>(.*?)</b></font>%', $ret, $regs)) {
				$err = $regs[1];
				ebaylog("\"{$err}\"");
			}
			
			test_write($ret);
			goto end;
		}
		
		
	} else {
		ebaylog("{$item}: Success signing in");
	}
	
	
	//place the inital bid
	curl_setopt($curl, CURLOPT_URL, "http://offer.ebay.com/ws/eBayISAPI.dll?MakeBid&item={$item}&maxbid={$bid}&campid=5337161990&customid=7");
	$ret = curl_exec ($curl);
	if(curl_errno($curl)){
		ebaylog('Curl error: ' . curl_error($curl));
	}
	if (!$ret) {
		$ret = curl_exec ($curl);
	}

	if (preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="stok"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs)) {
		$stok = $regs[1][0];
	} else {
		//Failed to get 'stok' value
		//try and determine why
		if (preg_match('%
<div class="statusDiv">(.*?)</div>

%', $ret, $regs)) {
			$err = $regs[1];
			ebaylog("'{$err}'");
			//if string starts with "Enter US $0.41 or more"
			if (stripos($err, 'Enter') === 0) {
				ebaylog("{$item}: 'Enter' found, aborting");
				//set it to success
				$success = true;
			} else if (stripos($err, 'To enter a') === 0) {
				ebaylog("{$item}: 'To enter a' found, aborting");
				//set it to success
				$success = true;
			} else if (stripos($err, 'Transaction Blocked') === 0) {
				ebaylog("{$item}: 'Transaction Blocked' found, aborting");
				//set it to success
				$success = true;
			}
			
		} else if (preg_match('%"\d*" - Invalid Item</div>

%', $subject)) {
			ebaylog("{$item}: 'Invalid Item' found, aborting");
			test_write($ret);
			//set it to success
			$success = true;
		} else if (preg_match('%
<div class="subTlt">
<ul class="errList">
<li>(.*?)</li>
</ul>
</div>

%', $subject)) {
			ebaylog("{$item}: 'no longer available' found, aborting");
			test_write($ret);
			//set it to success
			$success = true;
		} else if (preg_match('%id="w\d-\d-_msg".*?>(.*?)</span>%', $ret, $regs)) {
			ebaylog("'{$regs[1]}'");
		} else if (preg_match('%<div\s+class\s*=\s*"(?:errRed|errBlk|errTitle|statusDiv)"\s*>(.*?)</div>

%i', $ret, $regs)) {
			ebaylog("'{$regs[1]}'");
		} else {
			//don't know why so log the page
			ebaylog("{$item}: Failed to get 'stok' value");
			test_write($ret);
		}
		goto end;
	}
	
	if (preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="uiid"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs)) {
		$uiid = $regs[1][0];
	} else {
		ebaylog("{$item}: Failed to get 'uiid' value");
		goto end;
	}
	

	if ($stok && $uiid) {
		ebaylog("{$item}: Success placing initial bid");
	} else {
		ebaylog("{$item}: Failed placing initial bid");
		goto end;
		
	}
	
	//confirm the bid
	curl_setopt($curl, CURLOPT_URL, "http://offer.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeBid&maxbid={$bid}&quant=1&mode=1&stok={$stok}&uiid={$uiid}&co_partnerid=2&user={$username}&fb=0&item={$item}&campid=5337161990&customid=7");
	$ret = curl_exec ($curl);
	if(curl_errno($curl)){
		ebaylog('Curl error: ' . curl_error($curl));
	}
	if (!$ret) {
		$ret = curl_exec ($curl);
	}
	
	//perform a number of tests to determine if the bid was a success
	$bid_success = true;
	if (stripos($ret, "you're the first bidder") === FALSE) {
		if (stripos($ret, "you're the high bidder and currently in the lead") === FALSE) {
			if (stripos($ret, "you're currently the highest bidder") === FALSE) {
				$bid_success  = false;
				ebaylog("{$item}: Failed placing final bid");
				//try and determine why
				if (preg_match('%<div\s+class\s*=\s*"(?:errRed|errBlk|errTitle|statusDiv)"\s*>(.*?)</div>

%i', $ret, $regs)) {
					$err = $regs[1];
					ebaylog("'{$err}'");
					if (stripos($err, 'Enter') === 0) {
						ebaylog("{$item}: 'Enter' found, aborting");
						//set it to success
						$bid_success = true;
					} else if (stripos($err, "You've just been outbid") === 0) {
						ebaylog("{$item}: 'You've just been outbid' found, aborting");
						//set it to success
						$bid_success  = true;
					} else if (stripos($err, "You're currently the high bidder,") === 0) {
						ebaylog("{$item}: 'You're currently the high bidder, but the reserve hasn't been met.' found, aborting");
						//set it to success
						$bid_success  = true;
					}
				} else {
					//we don't know why it failed so write the data
					test_write($ret);
				}
			}
		}
	}
	
	if ($bid_success) {
		ebaylog("{$item}: Success placing final bid");
		$success = true;
	}
	
	end:
	
	//close the curl session
	curl_close ($curl);
	
	if ($success) {
		ebaylog("{$item}: Success: {$username}");
	} else {
		ebaylog("{$item}: Failure: {$username}");
	}
	
	return $success;
}

//for testing
function test_write($out) {
	$fh = fopen(dirname(__FILE__).'/'.date('Y-m-d H-i-s').'.html', 'w');
	fwrite($fh, $out);
	fclose($fh);
}

//simple logging function
function ebaylog($msg) {
	$msg = "{$msg}\r\n";
	echo $msg;
	$fh = fopen(dirname(__FILE__).'/ebay-log.txt', 'a');
	fwrite($fh, date('Y-m-d H-i-s')." {$msg}");
	fclose($fh);
}

PHP code to automatically bid/buy an eBay auction item

The following PHP code will automatically bid on an eBay item.

modify the following variables;

  • $username = “username”; //the eBay username
  • $password = “password”; //the eBay password
  • $item = 300712344201; //the item number
  • $bid =  0.01; //the  bid value in the item’s currency
<?php
	//modify the following

	$username = "username"; //the eBay username
	$password = "password"; //the eBay password

	$item = 300712344201; //the item number
	$bid =  0.01; //the  bid value in the item's currency

	//do not modify below

	//query the sign-in page
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
	curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
	curl_setopt($curl, CURLOPT_URL,"http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn");
	$ret = curl_exec ($curl);
	curl_close ($curl);

	//sign-in
	$post = "MfcISAPICommand=SignInWelcome&siteid=0&co_partnerId=2&UsingSSL=0&ru=&pp=&pa1=&pa2=&pa3=&i1=-1&pageType=-1&userid={$username}&pass={$password}";

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS,$post);
	curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
	curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
	curl_setopt($curl, CURLOPT_REFERER, "http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn");
	curl_setopt($curl, CURLOPT_URL, "http://signin.ebay.com/aw-cgi/eBayISAPI.dll");
	$ret = curl_exec ($curl);
	curl_close ($curl);

	if (strpos($ret, "Member id {$username}") === FALSE) {
		echo "Failed signing in.\r\n";
	} else {
		echo "Success signing in.\r\n";
	}

	//place the inital bid
	$url = "http://offer.ebay.com/ws/eBayISAPI.dll?MakeBid&item={$item}&maxbid={$bid}";

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
	curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
	curl_setopt($curl, CURLOPT_URL, $url);
	$ret = curl_exec ($curl);
	curl_close ($curl);

	preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="stok"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs);
	$stok = $regs[1][0];

	preg_match_all('/(?:value="([-0-9a-zA-Z]*)" *)?name="uiid"(?: *value="([-0-9a-zA-Z]*)")?/', $ret, $regs);
	$uiid = $regs[1][0];

	if (($stok) && ($uiid)) {
		echo "Success placing initial bid.\r\n";
	} else {
		echo "Failed placing initial bid.\r\n";
	}

	//confirm the bid
	$url = "http://offer.ebay.com/ws/eBayISAPI.dll?MfcISAPICommand=MakeBid&maxbid={$bid}&quant=1&mode=1&stok={$stok}&uiid={$uiid}&co_partnerid=2&user={$username}&fb=0&item={$item}";

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1");
	curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
	curl_setopt($curl, CURLOPT_COOKIEFILE, "cookies.txt");
	curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
	curl_setopt($curl, CURLOPT_URL, $url);
	$ret = curl_exec ($curl);
	curl_close ($curl);

	if (strpos($ret, "you're the first bidder") === FALSE) {
		echo "Failed placing final bid.\r\n";
	} else {
		echo "Success placing final bid.\r\n";
	}

	//for testing
	//$fh = fopen("testhtml.html", 'w');
	//fwrite($fh, $ret);
	//fclose($fh);
?>

PHP Factorial and Combination functions

A quick post on two functions for PHP that provide factorial and combination support.

function factorial($n) {
	if ($n <= 1) {
		return 1;
	} else {
		return factorial($n - 1) * $n;
	}
}

function combinations($n, $k) {
	//note this defualts to 0 if $n < $k
	if ($n < $k) {
		return 0;
	} else {
		return factorial($n)/(factorial($k)*factorial(($n - $k)));
	}
}