Saturday, September 30, 2017

Make nginx to pass hostname of the upstream when reverseproxying How to add a response header on nginx when using proxy_pass | Forward Custom Header from Nginx Reverse Proxy | Nginx request forward | Forward request using Nginx

Actually you can do that via proxy_set_header.

For more details look here: 

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header 


#Upstream is useful for same server
upstream localhost1 {
   server 127.0.0.1:80;  # this is new server, by IP address
}

server {
    listen   8500;
    server_name  localhost local.host;
    #access_log off;
    
    location / {
        proxy_pass http://dragon71.000webhostapp.com/;
        proxy_set_header    Nginx-Host             $host;
        proxy_set_header    Nginx-X-Real-IP        $remote_addr;
        proxy_set_header    Nginx-X-Forwarded-for  $remote_addr;
        proxy_set_header    Nginx-Target           base;
    
        location /pojo/ {
            proxy_pass http://localhost1/pkm/;
            proxy_set_header    Nginx-Host             $host;
            proxy_set_header    Nginx-X-Real-IP        $remote_addr;
            proxy_set_header    Nginx-X-Forwarded-for  $remote_addr;
            proxy_set_header    Nginx-Target           pojo;
        }
    }
    
    error_page  404               /404.html;
    error_page  500 502 503 504   /50x.html;
    location = /50x.html {
        root   html;
    }
}


And in php script:

Array
(
    [MIBDIRS] => ........
    [HTTP_HOST] => localhost
    [HTTP_NGINX_HOST] => localhost
    [HTTP_NGINX_X_REAL_IP] => 127.0.0.1
    [HTTP_NGINX_X_FORWARDED_FOR] => 127.0.0.1
    [HTTP_NGINX_TARGET] => base
    [HTTP_CACHE_CONTROL] => ....
)


If the proxy_pass directive is specified without a URI,



location /app/ {
    proxy_pass      http://127.0.0.1;
}

test.com/app/xxxxx =>  http://127.0.0.1/xxxxx

If the proxy_pass directive is specified with a URI:

location /app/ {
    proxy_pass      http://127.0.0.1/maped_dir/;
}

test.com/app/xxxxx =>  http://127.0.0.1/maped_dir/xxxxx

Forward the requested Host header:

By default, the Host header from the request is not forwarded, but is set based on the proxy_pass statement. To forward the requested Host header, it is necessary to use:

proxy_set_header Host $host;

If the location is given by regular expression, can not be a URI part in proxy_pass directive, unless there are variables in the directive.

location  ~ ^/app/(.*)$ {
    #proxy_pass   http://127.0.0.1/some_dir;    #error
    proxy_pass   http://127.0.0.1/some_dir/$1r;    #ok
}

variables in proxy_pass directive:

location ~ ^/app/(.*)$ {
    proxy_pass       http://127.0.0.1:$1;
}

test.com/app/8081 => http://127.0.0.1:8081

and:

location ~ ^/app/(.*)$ {
    proxy_pass       http://127.0.0.1:9999/some_dir/$1;
}

test.com/app/request/xxxxx => http://127.0.0.1:9999/some_dir/xxxxx

with a rewrite directive in the location:

If the rewrite rule is hit, the URI specified in the directive is ignored and the full changed request URI is passed to the server:

location  /app/ {
    rewrite    ^/app/hit/(.*)$ /hit_page.php?path=$1 break;
    proxy_pass   http://127.0.0.1:9999/some_dir/;
}
/app/hit/some/request/?name=xxxxx

=> http://127.0.0.1:9999/hit_page.php?path=some/request/&name=xxxxx

/app/not_hit/some/request/?name=xxxxx

=> http://127.0.0.1:9999/some_dir/not_hit/some/request/?name=xxxxx

Nginx Windows: How to Install | How to Install Nginx on Windows

At first you have to download Nginx from Nginx Downloads

Then extract it to c directory as below screenshot:




Then navigate to "nginx-1.9.9" and execute below command to start nginx server:

nginx.exe

Your server started at port 80 as http://localhost:80



To stop nginx server run below command:

nginx.exe -s stop

To change nginx port navigate to folder "config" folder and edit file "nginx.conf", find the string "listen       80;" and change it to suppose "listen       8200;", stop and start nginx, then browse to http://localhost:8200 as below:



Wednesday, September 27, 2017

Stripe Payment GateWay | Refund Stripe VIA API | Stripe API Refund Payment

Stripe Payment GateWay | Refund Stripe VIA API | Stripe API Refund Payment



<?php
include_once "curl.php";
$key = "sk_test_eXF4nNy........rcHK95rwj";

$params = array(
    "charge" => "ch_1B3bkMFIwfarG3vBaBs0hodp",
    "amount" => 10,
    "metadata" => array(
        "Refund-reason" => "Some reason to fail",
        "Refund-by" => "Pritom Kumaar"
    )
);

$url = "https://api.stripe.com/v1/refunds";

$headers[] = "Authorization: Bearer " . $key;
$headers[] = "Content-Type: application/x-www-form-urlencoded";

$response = CurlExecutor::execute($url, "POST", $params, null, $headers);
$response["response"] = json_decode($response["response"]);
CurlExecutor::prettyPrint($response);

And output would be like below:

Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => re_1B6h5DFIwfarG3vBvFwcbNl9
            [object] => refund
            [amount] => 10
            [balance_transaction] => txn_1B6h5DFIwfarG3vBALLuMSUw
            [charge] => ch_1B3bkMFIwfarG3vBaBs0hodp
            [created] => 1506524659
            [currency] => aud
            [metadata] => stdClass Object
                (
                    [Refund-reason] => Some reason to fail
                    [Refund-by] => Pritom Kumaar
                )

            [reason] => 
            [receipt_number] => 
            [status] => succeeded
        )

)

Monday, September 25, 2017

Clear previously set headers php | How to fix “Headers already sent” error in PHP

headers_sent indicates that it is too late to remove headers. They're already sent. Hence the name of the function.

What you want is to specifically check if the headers have not been sent yet. Then you know it's safe to modify them.


<?php
header('Content-type: application/csv');

if (!headers_sent()) {
    echo "<pre>";
    print_r(headers_list());
    echo "</pre>";
}

header_remove();
header('Content-type: text/html');
echo "HERE";


Array
(
    [0] => X-Powered-By: PHP/7.0.9
    [1] => Content-type: application/csv
)
HERE

Monday, September 18, 2017

Stripe Using Connect with Deferred Standard Accounts | Stripe Create Deferred Standard Account | Stripe Create Connected Account Using API Call

1 Login to your stripe account and navigate to https://dashboard.stripe.com/account/applications/settings and follow below steps:









2. Now have to create Deferred (standard) account using below code snippet (API call). Later account holder will login to their account and complete their verification process:



<?php
include_once "curl.php";

$api_key = "sk_test_eXF..........jWrcHK95rwj";
$url = "https://api.stripe.com/v1/accounts";

$params = array(
    "type" => "standard",
    "country" => "AU",
    "email" => "pritomkucse+stripe1@gmail.com"
);

$headers[] = "Authorization: Bearer $api_key";
$headers[] = "Content-Type: application/x-www-form-urlencoded";

$result = CurlExecutor::execute($url, "POST", null, $params, $headers);
$result["response"] = json_decode($result["response"]);
CurlExecutor::prettyPrint($result);

Successful output would be like below:


Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [id] => acct_1B3......1R7ddx6
            [object] => account
            [business_logo] => 
            [business_name] => 
            [business_url] => 
            [charges_enabled] => 1
            [country] => AU
            [default_currency] => aud
            [details_submitted] => 
            [display_name] => 
            [email] => pritomkucse+stripe1@gmail.com
            [keys] => stdClass Object
                (
                    [secret] => sk_test_wel..........5fXfyBsmhI6
                    [publishable] => pk_test_mlPoU..........zBUbHBFof
                )

            [metadata] => stdClass Object
                (
                )

            [payouts_enabled] => 
            [statement_descriptor] => 
            [support_email] => 
            [support_phone] => 
            [timezone] => Etc/UTC
            [type] => standard
        )

)

Use "keys/secret" (actually it is "Secret key" for API), you can use this Key to process all your transactions.

And list of your connected account is below:




Stripe Using Connect with Standard Accounts | Stripe Create Standard Account

1 Login to your stripe account and navigate to https://dashboard.stripe.com/account/applications/settings and follow below steps:








2. Copy Client ID and follow below PHP script (which will redirect target users to Stripe application page to fill up their personal details to create and account with Stripe):



<?php
$client_id = "ca_BPnCyPD...........ODApFdHyTXHbwQ";
$auth_url = "https://connect.stripe.com/oauth/authorize?".
    "response_type=code&client_id=$client_id&scope=read_write".
    "&redirect_uri=".rawurlencode("http://localhost/ci/dragon71/stripe/callback.php").
    "&state=".md5(trim(true));
header("Refresh:0; url=$auth_url");

3. After completing procedures there, Stripe will redirect back to our target redirect URL as below:


http://localhost/ci/dragon71/stripe/callback.php?state=c4ca4238a0b923820dcc509a6f75849b&scope=read_write&code=ac_BQGZyHtVkFbOFYhNPTlAdbZeZcMutzec

If fails then:

http://localhost/ci/dragon71/stripe/callback.php?state=c4ca4238a0b923820dcc509a6f75849b&error=access_denied&error_description=The+user+denied+your+request

4. One thing we forgot, have to collect our API Key to request API call, navigate to https://dashboard.stripe.com/account/apikeys to collect your API Key's.




5. After successful redirect back, it's time to collect access token to procedure further. 


<?php
include_once "curl.php";

$api_key = "sk_test_eXF4.........jWrcHK95rwj";
$url = "https://connect.stripe.com/oauth/token";

$params = array(
    "code" => $_GET["code"],
    "grant_type" => "authorization_code"
);

$headers[] = "Authorization: Bearer $api_key";
$headers[] = "Content-Type: application/x-www-form-urlencoded";

$result = CurlExecutor::execute($url, "POST", null, $params, $headers);
$result["response"] = json_decode($result["response"]);
CurlExecutor::prettyPrint($result);

Successful output would be like below:


Array
(
    [code] => 200
    [response] => stdClass Object
        (
            [access_token] => sk_test_oWtb.........xb4tLBXqZmh
            [livemode] => 
            [refresh_token] => rt_BQGsyumxyAoDH....................aPMt5MvpprSnpwD
            [token_type] => bearer
            [stripe_publishable_key] => pk_test_xHhgw.........zfo0QYltBB
            [stripe_user_id] => acct_1B3........3aeEh
            [scope] => read_write
        )

)

Use "access_token" (actually it is "Secret key" for API), you can use this Key to process all your transactions.

And list of your connected account is below:




Sunday, September 10, 2017

The Complete Guide to Creating Symbolic Links | How to create a Symbolic Link on Windows

mklink creates a symbolic link to a file. The below command creates a symbolic, or “soft”, link at Link pointing to the file Target :

mklink "c:\tmp\a.txt" "c:\tmp\b.txt"

will create link a.txt (destination) that is equal to b.txt (source)

Use /D to create soft link pointing to a directory. like so:

mklink /D "c:\tmp\dir_destination" "c:\tmp\dir_source"

Use /H to create hard link pointing to a file:

mklink /H "c:\tmp\dir_destination" "c:\tmp\dir_source"

Use /J to create hard link pointing to a directory, also known as a directory junction:


mklink /J "c:\tmp\dir_destination" "c:\tmp\dir_source"

mklink /J Link Target

And below command will remove symbolic link on a drive:

rmdir "c:\tmp\dir_destination"