Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Thursday, September 28, 2017

Coinhive: Capcha Example: Using PHP

<html>
<head></head>
<body>
<div>
<?php
if(isset($_POST["coinhive-captcha-token"])){
        $token = $_POST["coinhive-captcha-token"];
        $secret = 'bAbbbb5A9AbA0bbbA3bAAbAb8bAbAAAb';
        $url = 'https://api.coinhive.com/token/verify';

        $data = array('token'=> $token,
                      'hashes' => 1024,
                      'secret' => $secret);

        $options = array(
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data)
        );
     
        $context  = stream_context_create(array('http' => $options));

        $response = json_decode(file_get_contents($url, false, $context));

        if ($result === FALSE) {
                echo "Error";
                exit;
        }
     
        $responseSuccess = $response->success;
}
?>
<?php if ($responseSuccess){ ?>
Verified<br/>
<?php } else { ?>
You must verify
<form action="?" method="post">
        <!-- other form fields -->

        <script src="https://coinhive.com/lib/captcha.min.js" async></script>
        <div class="coinhive-captcha" data-hashes="1024" data-key="AAAbAA33bAAbAbbAbAA4bA7dA4AAbA7b"></div>

        <input type="submit" value="Submit"/>
</form>
<?php } ?>
</div>
</body>
</html>

Monday, October 3, 2016

macOS Sierra: Facebook: Post Using PHP

Command:

$ cat /Library/WebServer/Documents/fb-tokens/index.php


Result:

<?php
print_r($_GET);

?>


URL (Get code):

https://graph.facebook.com/oauth/authorize?client_id=MY_APP_ID&scope=publish_actions&redirect_uri=http://localhost/fb-tokens/


Result:

Array ( [code] => MY_CODE )


URL (GET access token):

https://graph.facebook.com/oauth/access_token?client_id=MY_APP_ID&redirect_uri=http://localhost/fb-tokens/&client_secret=MY_APP_SECRET&code=MY_CODE


Result:

access_token=MY_ACCESS_TOKEN&expires=5176951


Command:

$ cat posttest.php


Result:

<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once("vendor/facebook/php-sdk/src/Facebook/Facebook.php");
use Facebook\Facebook;

$config = array();
$config['app_id'] = 'MY_APP_ID';
$config['app_secret'] = 'MY_APP_SECRET';
$config['fileUpload'] = false; // optional

$fb = new Facebook($config);
 
$params = array(
"access_token" => "MY_ACCESS_TOKEN",
"message" => "My first post using php",
"link" => "http://nautil.us/issue/40/learning/is-artificial-intelligence-permanently-inscrutable",
"picture" => "http://static.nautil.us/10275_3ac01363eb96084101c0e5ed8be7374a.png",
"name" => "Is Artificial Intelligence Permanently Inscrutable?",
"caption" => "nautil.us",
"description" => "Despite new biology-like tools, some insist interpretation is impossible."
);
               
// post to Facebook
try {
$ret = $fb->post('/me/feed', $params);
echo 'Successfully posted to Facebook';
} catch(Exception $e) {
echo $e->getMessage();
}
?>

macOS Sierra: Apache: Enable PHP and User Directory

Command:

$ sudo vi /etc/apache2/httpd.conf
$ diff /etc/apache2/httpd.conf.original /etc/apache2/httpd.conf


Result:

166c166
< #LoadModule userdir_module libexec/apache2/mod_userdir.so
---
> LoadModule userdir_module libexec/apache2/mod_userdir.so
169c169
< #LoadModule php5_module libexec/apache2/libphp5.so
---
> LoadModule php5_module libexec/apache2/libphp5.so


Command:

$ sudo apachectl restart

PHP: Installing Facebook SDK for PHP using Composer

Command:

$ cat composer.json


Result:

{
 "require" : {
  "facebook/php-sdk" : "*"
 }
}


Command:

$ php composer.phar install


Result:

Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing facebook/php-sdk (5.3.1)
    Downloading: 100%      

facebook/php-sdk suggests installing guzzlehttp/guzzle (Allows for implementation of the Guzzle HTTP client)
Package facebook/php-sdk is abandoned, you should avoid using it. Use facebook/graph-sdk instead.
Writing lock file
Generating autoload files


Command:

$ tree vendor/


Result:

vendor/
├── autoload.php
├── composer
│   ├── ClassLoader.php
│   ├── LICENSE
│   ├── autoload_classmap.php
│   ├── autoload_files.php
│   ├── autoload_namespaces.php
│   ├── autoload_psr4.php
│   ├── autoload_real.php
│   ├── autoload_static.php
│   └── installed.json
└── facebook
    └── php-sdk
        ├── LICENSE
        ├── composer.json
        ├── phpcs.xml.dist
        └── src
            └── Facebook
                ├── Authentication
                │   ├── AccessToken.php
                │   ├── AccessTokenMetadata.php
                │   └── OAuth2Client.php
                ├── Exceptions
                │   ├── FacebookAuthenticationException.php
                │   ├── FacebookAuthorizationException.php
                │   ├── FacebookClientException.php
                │   ├── FacebookOtherException.php
                │   ├── FacebookResponseException.php
                │   ├── FacebookResumableUploadException.php
                │   ├── FacebookSDKException.php
                │   ├── FacebookServerException.php
                │   └── FacebookThrottleException.php
                ├── Facebook.php
                ├── FacebookApp.php
                ├── FacebookBatchRequest.php
                ├── FacebookBatchResponse.php
                ├── FacebookClient.php
                ├── FacebookRequest.php
                ├── FacebookResponse.php
                ├── FileUpload
                │   ├── FacebookFile.php
                │   ├── FacebookResumableUploader.php
                │   ├── FacebookTransferChunk.php
                │   ├── FacebookVideo.php
                │   └── Mimetypes.php
                ├── GraphNodes
                │   ├── Birthday.php
                │   ├── Collection.php
                │   ├── GraphAchievement.php
                │   ├── GraphAlbum.php
                │   ├── GraphApplication.php
                │   ├── GraphCoverPhoto.php
                │   ├── GraphEdge.php
                │   ├── GraphEvent.php
                │   ├── GraphGroup.php
                │   ├── GraphList.php
                │   ├── GraphLocation.php
                │   ├── GraphNode.php
                │   ├── GraphNodeFactory.php
                │   ├── GraphObject.php
                │   ├── GraphObjectFactory.php
                │   ├── GraphPage.php
                │   ├── GraphPicture.php
                │   ├── GraphSessionInfo.php
                │   └── GraphUser.php
                ├── Helpers
                │   ├── FacebookCanvasHelper.php
                │   ├── FacebookJavaScriptHelper.php
                │   ├── FacebookPageTabHelper.php
                │   ├── FacebookRedirectLoginHelper.php
                │   └── FacebookSignedRequestFromInputHelper.php
                ├── Http
                │   ├── GraphRawResponse.php
                │   ├── RequestBodyInterface.php
                │   ├── RequestBodyMultipart.php
                │   └── RequestBodyUrlEncoded.php
                ├── HttpClients
                │   ├── FacebookCurl.php
                │   ├── FacebookCurlHttpClient.php
                │   ├── FacebookGuzzleHttpClient.php
                │   ├── FacebookHttpClientInterface.php
                │   ├── FacebookStream.php
                │   ├── FacebookStreamHttpClient.php
                │   ├── HttpClientsFactory.php
                │   └── certs
                │       └── DigiCertHighAssuranceEVRootCA.pem
                ├── PersistentData
                │   ├── FacebookMemoryPersistentDataHandler.php
                │   ├── FacebookSessionPersistentDataHandler.php
                │   ├── PersistentDataFactory.php
                │   └── PersistentDataInterface.php
                ├── PseudoRandomString
                │   ├── McryptPseudoRandomStringGenerator.php
                │   ├── OpenSslPseudoRandomStringGenerator.php
                │   ├── PseudoRandomStringGeneratorFactory.php
                │   ├── PseudoRandomStringGeneratorInterface.php
                │   ├── PseudoRandomStringGeneratorTrait.php
                │   └── UrandomPseudoRandomStringGenerator.php
                ├── SignedRequest.php
                ├── Url
                │   ├── FacebookUrlDetectionHandler.php
                │   ├── FacebookUrlManipulator.php
                │   └── UrlDetectionInterface.php
                ├── autoload.php
                └── polyfills.php

16 directories, 89 files



PHP: Composer: Downloading and Installing Composer

Command:

$ wget -O composer-setup.php https://getcomposer.org/installer


Result:

--2016-10-03 17:22:22--  https://getcomposer.org/installer
Resolving getcomposer.org... 2001:41d0:a:7b19::2, 87.98.253.108
Connecting to getcomposer.org|2001:41d0:a:7b19::2|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 293630 (287K) [application/octet-stream]
Saving to: ‘composer-setup.php’

composer-setup.php         100%[=======================================>] 286.75K   185KB/s    in 1.5s  

2016-10-03 17:22:26 (185 KB/s) - ‘composer-setup.php’ saved [293630/293630]


Command:

$ php -r "if (hash_file('SHA384', 'composer-setup.php') === 'e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"


Result:

Installer verified


Command:

$ php composer-setup.php


Result:

All settings correct for using Composer
Downloading 1.2.1...

Composer successfully installed to: /Users/USERNAME/composer.phar
Use it: php composer.phar

Friday, February 5, 2016

Version of PHP

$ php --version
PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

Thursday, July 16, 2015

PHP: Snippet: 任意のURLパラメータの値を取得


// Urlのパラメータの値を見つけて取得。?page_id%3D100&param2%3D200となっている場合、$pageIDは100になる。
   if (preg_match('/page_id%3D([0-9]+)/', $line, $match))
   {
      $pageID = $match[1];
   }

PHP: Snippet: 1行内の任意の標識文字に対する値の取得

// Label: の後に書いてある文字列を取得する
if (mb_strpos($line, “Label:”) !== false)
{
   $str = mb_substr($line, mb_strpos($line, “Label:”) + strlen(“Label:“));
}

PHP: Snippet: XML Parsing Example

// XMLを取得
$myXMLData = file_get_contents($xmlURL . $xmlURLParameter);

// XMLをパース
$xml = simplexml_load_string($myXMLData) or die (“Error: Cannot create object”);

// タイトルを取得
$title = $xml->entry->title;

// 概要を取得
$summary = $xml->entry->summary;

// 概要を行で分解
$lines = explode(PHP_EOL, $summary);

PHP: function: isToday_new

// 今日か?
function isToday_new($y, $m, $d)
{  
   if (date('Y-m-d') == date ('Y-m-d', strtotime($y . "-" . $m . "-" . $d)))
   {
      $retval = true;
   }
   
   return $retval;
}

PHP: function: monthENtoJP

// 月を英語から数値へ
function monthENtoJP($en)
{
   $monthENtoNum = array(
      "Jan" => 1 , 
      "Feb" => 2 , 
      "Mar" => 3, 
      "Apr" => 4, 
      "May" => 5, 
      "Jun" => 6, 
      "Jul" => 7, 
      "Aug" => 8, 
      "Sep" => 9, 
      "Oct"=> 10, 
      "Nov" => 11, 
      "Dec" => 12);

   return $monthENtoNum[$en];
}

PHP: function: weekdayENtoJP

// 曜日を英語から日本語へ
function weekdayENtoJP($en)
{
   $weekdayENtoJP = array(
      "Sun" => "日",
      "Mon" => "月",
      "Tue" => "火",
      "Wed" => "水",
      "Thu" => "木",
      "Fri" => "金",
      "Sat" => "土");

   return $weekdayENtoJP[$en];
}