Category Archives: Programming 程式碼

Post JSON to web api in PHP

[Action] POST JSON format

{
    "command": "login",
    "params": {
        "username": "username",
        "password": "password"
    }
}

[Action] Return POST result

{
    "success": true
}

index.php

$data = array(
  'command' => 'login',
  'params' => array(
        'username' => 'username',
        'password' => 'password'    
  )
);
json_encode( $data );

$api_url = 'https://study.ar-leung.net';
$ch = curl_init( $api_url );

# Setup request to send json via POST.
$payload = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

# Send request.
$result = curl_exec($ch);
curl_close($ch);

# Print response.
echo "<pre>$result</pre>";

$json_data = json_decode($result);
echo $json_data->success;

curl_get_file_contents

randIP

function randIP(){	
		$pass = false;
		while(true){
			$ip = rand(1, 255);		
			if($ip != 192 || $ip != 172 || $ip != 10 || $ip != 127 ){
				$pass = true;
				return $ip;
			}
		}	
	}

curl_get_file_contents

	function curl_get_file_contents($curl, $ref_url){
		$genIP = randIP().".".randIP().".".randIP().".".randIP();
		$headers['CLIENT-IP'] = $genIP;  
		$headers['X-FORWARDED-FOR'] = $genIP; 
		
		$headerArr = array();  
		foreach( $headers as $n => $v ) {  
			$headerArr[] = $n .':' . $v;   
		}
		
		$c = curl_init();
		curl_setopt($c, CURLOPT_HEADER, 0);
		curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
		curl_setopt($c, CURLOPT_REFERER, $ref_url);		
		curl_setopt($c, CURLOPT_HTTPHEADER , $headerArr );
		curl_setopt($c, CURLOPT_URL, $url);
		
		$contents = curl_exec($c);
		curl_close($c);

		if ($contents) return $contents;

使用不同的 IP 存取別人的網頁,舉例:

curl_get_file_contents("http://domain/img.jpg", "http://domain/index.html");

curlHTML

curlHTML

function curlHTML($durl) {
	$ch = curl_init();
	$cookie = false;
	curl_setopt($ch, CURLOPT_URL, $durl);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	if($cookie) {
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	}
	#curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
	#curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$r = curl_exec($ch);

	curl_close($ch);
	return $html_code = $r;
}

Database Class

<?php
class Database{
	
		protected $host;
		protected $user;
		protected $pass;
		protected $database;
		protected $port;
		
		function Database($host, $user, $pass, $database, $port){
			$this->host = $host;
			$this->user = $user;
			$this->pass = $pass;
			$this->database = $database;
			$this->port = $port;
		}		
		
		/** Connection Funtion **/
		function conn(){
			$this->conn = mysqli_connect($this->host. ":".$this->port, $this->user, $this->pass)or die("Invalid database");
			mysqli_select_db($this->conn, $this->database) or die("Can not connect database");
			mysqli_query($this->conn, "SET NAMES 'utf8'");
			date_default_timezone_set('Hongkong');
			return "Connect database succeed";
		}
	
		function close() {
			mysqli_close($this->conn);
		}
		
		function error() {
			return mysqli_error($this->conn);
		}
		
		/** Execution Query Function **/ 
		function query($query) {
			$this->query = $query;
			return $this->result = mysqli_query($this->conn, $this->query) or die(error());
		}
		
		function insert_id() {
			return mysqli_insert_id($this->conn);
		}
		
		function num_rows() {
			return mysqli_num_rows($this->result);
		}
		
		function free_result() {
			mysqli_free_result($this->result) ;
		}
		
		function fetch_array($mode = MYSQLI_ASSOC) {
			return mysqli_fetch_array($this->result, $mode);
		}
		
		function affected_rows() {
			return mysqli_affected_rows($this->conn);
		}
		
		/** Function **/		
		function escape_string($string) {
			if (get_magic_quotes_gpc()) {
				$string = stripslashes($string);
			}
			if (!is_numeric($string)) {
				$string = mysqli_real_escape_string($this->conn, $string);
			}
			return trim($string);
		}

		/*
			function insert_escape_string($string) {
				return $this->real_escape_string($string, true); 
			}
		*/
	
		function uniqid($length = 3){
			$array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
			0,1,2,3,5,6,7,8,9);

			$key = "";
			$size = sizeof($array)-1;
			for($i = $length; $i > 0;$i--) {
				$n = rand(0, $size);
				$key.= $array[$n];
			}
			
			$uniqid = uniqid();
			$uniqid .= md5($uniqid.$key);
			return $uniqid;
		}	
	}
?>

Tutorial

$host 	  = "127.0.0.1";
$user 	  = "root";
$pass	  = "";
$db 	  = "test";
$port	  = "3306";
$lang     = "zh-tw";
		
$database = new Database($host, $user, $pass, $db, $port);	
$database->conn();

$sql = "SELECT * FROM test";
$result = $database->query($sql);
$rows = $database->fetch_array($result);
$database->free_result();