<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Crumbs Archives - caskiSPACE</title>
	<atom:link href="https://www.caskispace.com/category/code-crumbs/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.caskispace.com/category/code-crumbs/</link>
	<description>Tools, tips and tutes</description>
	<lastBuildDate>Wed, 17 May 2023 04:49:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.caskispace.com/wp-content/uploads/2019/12/favicon-64.png</url>
	<title>Code Crumbs Archives - caskiSPACE</title>
	<link>https://www.caskispace.com/category/code-crumbs/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>AWS: Step Functions</title>
		<link>https://www.caskispace.com/aws-step-functions/</link>
					<comments>https://www.caskispace.com/aws-step-functions/#respond</comments>
		
		<dc:creator><![CDATA[saborot]]></dc:creator>
		<pubDate>Wed, 17 May 2023 02:41:35 +0000</pubDate>
				<category><![CDATA[Code Crumbs]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[State Machine]]></category>
		<category><![CDATA[Step Functions]]></category>
		<guid isPermaLink="false">https://www.caskispace.com/?p=12725</guid>

					<description><![CDATA[<p>How to Add Your Step Functions Inside Cloud Formation I recently had to create a series of step functions for a project I am working on. The beauty of using step function is you get to work on it visually; from adding of functions, states, inputs and outputs up to real-time running and debugging. It [&#8230;]</p>
<p>The post <a href="https://www.caskispace.com/aws-step-functions/">AWS: Step Functions</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">How to Add Your Step Functions Inside Cloud Formation</h2>



<p class="wp-block-paragraph">I recently had to create a series of step functions for a project I am working on. The beauty of using step function is you get to work on it visually; from adding of functions, states, inputs and outputs up to real-time running and debugging.</p>



<p class="wp-block-paragraph">It is awesome! However, after the wow factor, one thing that came to mind was &#8220;How am I going to get this inside a repository so it can be versioned?&#8221;. Turns out, I can actually add all of it inside a CloudFormation! Meaning in my application, I can create a YAML file, add it in there and it will generate the State Machine which has all of my step functions inside.</p>



<p class="wp-block-paragraph">First I needed a policy so I can perform Step Function actions from within my application. </p>



<div class="wp-block-group has-white-color has-black-background-color has-text-color has-background"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<pre class="wp-block-code has-white-color has-text-color has-small-font-size"><code>
  Resources:
    MyAppRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - states.amazonaws.com
              Action:
                - sts:AssumeRole
          - PolicyName: StepFunctions
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
              - Effect: Allow
                Action:
                  - states:*
                Resource: '*'
</code></pre>
</div></div>



<p class="wp-block-paragraph">Then I added the following inside my <em>template.yaml</em> file under <em>Resources</em> section:</p>



<div class="wp-block-group has-pale-cyan-blue-background-color has-background has-small-font-size"><div class="wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained">
<pre class="wp-block-code has-white-color has-black-background-color has-text-color has-background"><code>
  Resources:
    MyStateMachine:
      Type: AWS::StepFunctions::StateMachine
      Properties:
        StateMachineName: MyStateMachineName
        RoleArn: !GetAtt MyAppRole.Arn
        DefinitionString:
          Fn::Sub: |
            {
            ... STEP FUNCTION DEFINITION HERE
            }
</code></pre>
</div></div>
<p>The post <a href="https://www.caskispace.com/aws-step-functions/">AWS: Step Functions</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.caskispace.com/aws-step-functions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Call ChromeData in NodeJS and PHP</title>
		<link>https://www.caskispace.com/call-chromedata-in-nodejs-and-php/</link>
		
		<dc:creator><![CDATA[saborot]]></dc:creator>
		<pubDate>Sat, 06 Nov 2021 07:34:52 +0000</pubDate>
				<category><![CDATA[Code Crumbs]]></category>
		<guid isPermaLink="false">https://www.caskispace.com/?p=11875</guid>

					<description><![CDATA[<p>In this code crumb, we will call ChromeData in NodeJS and PHP. We will call the &#8220;describeVehicle&#8221; method of ChromeData ADS(Automotive Description Service). The &#8220;describeVehicle&#8221; method accepts a vehicle&#8217;s VIN, searches its database for a match then responds with information for that vehicle. ChromeData uses WSDL and one way for us to make use of its [&#8230;]</p>
<p>The post <a href="https://www.caskispace.com/call-chromedata-in-nodejs-and-php/">Call ChromeData in NodeJS and PHP</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></description>
										<content:encoded><![CDATA[		<div data-elementor-type="wp-post" data-elementor-id="11875" class="elementor elementor-11875" data-elementor-post-type="post">
						<section class="elementor-section elementor-top-section elementor-element elementor-element-3099518 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="3099518" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-6e483f3" data-id="6e483f3" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-fb3a36e elementor-widget elementor-widget-text-editor" data-id="fb3a36e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Sans-serif; font-size: 1rem;">In this code crumb, we will call ChromeData in NodeJS and PHP.</span></p><p><span style="color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Sans-serif; font-size: 1rem;">We will call the &#8220;describeVehicle&#8221; method of ChromeData ADS(</span>Automotive Description Service<span style="color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Sans-serif; font-size: 1rem;">). The &#8220;describeVehicle&#8221; method accepts a vehicle&#8217;s VIN, searches its database for a match then responds with information for that vehicle.</span></p><p><span style="color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Sans-serif; font-size: 1rem;">ChromeData uses WSDL and one way for us to make use of its services is to connect to it by passing our credentials along with our request parameters using SOAP(Simple Object Access Protocol).</span></p><p><span style="color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Sans-serif; font-size: 1rem;">The process is pretty much the same between languages, however there is one key difference when passing credentials using NodeJS &#8220;node-soap&#8221; module. It seems that in NodeJS, we need to explicitly tell the module  to treat the credentials(number, secret, country, language) as attributes of the AccountInfo XML element instead of children. This is something we don&#8217;t have to explicitly define when passing our request parameters to SoapClient in PHP.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-308e326 elementor-widget elementor-widget-template" data-id="308e326" data-element_type="widget" data-widget_type="template.default">
				<div class="elementor-widget-container">
							<div class="elementor-template">
					<div data-elementor-type="section" data-elementor-id="311" class="elementor elementor-311" data-elementor-post-type="elementor_library">
					<section class="elementor-section elementor-top-section elementor-element elementor-element-1c9461bb elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="1c9461bb" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3c156f23" data-id="3c156f23" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-4ad0f189 elementor-widget elementor-widget-html" data-id="4ad0f189" data-element_type="widget" data-widget_type="html.default">
				<div class="elementor-widget-container">
					<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Single-row Horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-7317245021228731"
     data-ad-slot="2931534497"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>				</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				</div>
				</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-f44ad9e elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="f44ad9e" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3355e5e" data-id="3355e5e" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-e3a07ac elementor-widget elementor-widget-heading" data-id="e3a07ac" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-default">Using PHP</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-f0672c9 elementor-widget elementor-widget-accordion" data-id="f0672c9" data-element_type="widget" data-widget_type="accordion.default">
				<div class="elementor-widget-container">
							<div class="elementor-accordion">
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2521" class="elementor-tab-title" data-tab="1" role="button" aria-controls="elementor-tab-content-2521" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 1. Prepare Request Parameters</a>
					</div>
					<div id="elementor-tab-content-2521" class="elementor-tab-content elementor-clearfix" data-tab="1" role="region" aria-labelledby="elementor-tab-title-2521"><h4>Prepare Request Parameters</h4>

<div class="code color-green">
<pre>
$url = 'http://services.chromedata.com/Description/7c?wsdl';
$request_params = [
    'accountInfo' => [
        'number'=>'12345',
        'secret'=>'A12B345C67890123',
        'country'=>'US',
        'language' =>'en'    
    ],
    'vin' => '5GAKVCED2CJ310674',
];
</pre>
</div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2522" class="elementor-tab-title" data-tab="2" role="button" aria-controls="elementor-tab-content-2522" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 2. Create a SOAP Client</a>
					</div>
					<div id="elementor-tab-content-2522" class="elementor-tab-content elementor-clearfix" data-tab="2" role="region" aria-labelledby="elementor-tab-title-2522"><h4>Step 2. Create a SOAP Client</h4><p>References: <a href="https://www.php.net/manual/en/class.soapclient.php" target="_blank" rel="noopener">SoapClient</a></p><hr /><div class="code color-green"><pre>try {
    $client = new SoapClient($url);
} catch (SoapFault $e) {
    $msg = 'chrome_ads_get_client -- Could not get client. ' . $e-&gt;getMessage();
}
</pre></div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2523" class="elementor-tab-title" data-tab="3" role="button" aria-controls="elementor-tab-content-2523" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 3. Call the ChromeData ADS "describeVehicle" Method</a>
					</div>
					<div id="elementor-tab-content-2523" class="elementor-tab-content elementor-clearfix" data-tab="3" role="region" aria-labelledby="elementor-tab-title-2523"><h4>Call the ChromeData ADS &#8220;describeVehicle&#8221; Method</h4>

<div class="code color-green">
<pre>
try {
    $response = $client->__soapCall('describeVehicle', [$request_params]);
} catch (Exception $e) {
    $msg = 'chrome_ads_describe_vehicle -- Could not get data. ' . $e->getMessage();
}
</pre>
</div></div>
				</div>
								</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-f0c694b elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="f0c694b" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-be7c884" data-id="be7c884" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-5987ec3 elementor-widget elementor-widget-spacer" data-id="5987ec3" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-a453da2 elementor-widget elementor-widget-heading" data-id="a453da2" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-default">Using Node JS</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-296b74c elementor-widget elementor-widget-accordion" data-id="296b74c" data-element_type="widget" data-widget_type="accordion.default">
				<div class="elementor-widget-container">
							<div class="elementor-accordion">
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-4341" class="elementor-tab-title" data-tab="1" role="button" aria-controls="elementor-tab-content-4341" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 1. Prepare Request Parameters</a>
					</div>
					<div id="elementor-tab-content-4341" class="elementor-tab-content elementor-clearfix" data-tab="1" role="region" aria-labelledby="elementor-tab-title-4341"><h4>Prepare Request Parameters</h4><p><span class="color-red"><strong>NOTE:</strong> </span>The &#8220;AccountInfo&#8221; parameter here is written differently than how we are doing it on PHP. In the code below, we are explicitly defining that <strong>&#8220;number&#8221;, &#8220;secret&#8221;, &#8220;country&#8221; and &#8220;language&#8221; are attributes of &#8220;AccountInfo&#8221;</strong>. If we do not do it this way, the resulting request XML would treat those as children of AccountInfo rather than attributes resulting in an <strong>&#8220;Unable to authenticate account &#8211;**UNKNOWN**&#8211;&#8220;</strong> error.</p><hr /><div class="code color-green"><pre>const url = 'http://services.chromedata.com/Description/7c?wsdl';
const requestParams = {
    accountInfo: {
        attributes: {
            number: '12345',
            secret: 'A12B345C67890123',
            country: 'US',
            language: 'en'
        }
    },
    vin: '5GAKVCED2CJ310674,
};
</pre></div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-4342" class="elementor-tab-title" data-tab="2" role="button" aria-controls="elementor-tab-content-4342" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 2. Install Required Modules</a>
					</div>
					<div id="elementor-tab-content-4342" class="elementor-tab-content elementor-clearfix" data-tab="2" role="region" aria-labelledby="elementor-tab-title-4342"><h4>Install Required Modules</h4>
References: <a href="https://www.php.net/manual/en/class.soapclient.php" target="_blank" rel="noopener">vpulim/node-soap</a>, <a href="https://www.npmjs.com/package/@types/sax" target="_blank" rel="noopener">@types/Sax</a>

<hr />

<br>In your command line interface, install &#8220;soap&#8221; and it&#8217;s build dependency &#8220;@types/sax&#8221;

<div class="code color-green">
<pre>
npm install soap
npm install --save-dev @types/sax
</pre>
</div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-4343" class="elementor-tab-title" data-tab="3" role="button" aria-controls="elementor-tab-content-4343" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 3. Create a SOAP Client</a>
					</div>
					<div id="elementor-tab-content-4343" class="elementor-tab-content elementor-clearfix" data-tab="3" role="region" aria-labelledby="elementor-tab-title-4343"><h4>Create a SOAP Client</h4>

<div class="code color-green">
<pre>
const soap = require('soap');
const soapClient = await soap.createClientAsync(url);
</pre>
</div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-4344" class="elementor-tab-title" data-tab="4" role="button" aria-controls="elementor-tab-content-4344" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 4. Call the ChromeData ADS "describeVehicle" Method</a>
					</div>
					<div id="elementor-tab-content-4344" class="elementor-tab-content elementor-clearfix" data-tab="4" role="region" aria-labelledby="elementor-tab-title-4344"><h4>Call the ChromeData ADS &#8220;describeVehicle&#8221; Method</h4><div class="code color-green"><pre>const results = await soapClient['describeVehicleAsync']([<span style="font-size: 1em;">requestParams</span><span style="font-size: 1em;">]);</span></pre></div></div>
				</div>
								</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-7fc21f9 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="7fc21f9" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b756090" data-id="b756090" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-56a9374 elementor-widget elementor-widget-spacer" data-id="56a9374" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				</div>
		<p>The post <a href="https://www.caskispace.com/call-chromedata-in-nodejs-and-php/">Call ChromeData in NodeJS and PHP</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Upload Ads.txt to Bitnami Web Server</title>
		<link>https://www.caskispace.com/upload-ads-txt-to-bitnami-web-server/</link>
		
		<dc:creator><![CDATA[saborot]]></dc:creator>
		<pubDate>Tue, 02 Nov 2021 05:02:56 +0000</pubDate>
				<category><![CDATA[Code Crumbs]]></category>
		<guid isPermaLink="false">https://www.caskispace.com/?p=11854</guid>

					<description><![CDATA[<p>This seems easy but I spent some time googling it on the internet and did not find a straight answer.  I had to move my WordPress site to an AWS LightSail due to some technical issue with my previous web host and I didn&#8217;t know where to put my Ads.txt.  To cut things short, AWS [&#8230;]</p>
<p>The post <a href="https://www.caskispace.com/upload-ads-txt-to-bitnami-web-server/">Upload Ads.txt to Bitnami Web Server</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></description>
										<content:encoded><![CDATA[		<div data-elementor-type="wp-post" data-elementor-id="11854" class="elementor elementor-11854" data-elementor-post-type="post">
						<section class="elementor-section elementor-top-section elementor-element elementor-element-3099518 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="3099518" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-6e483f3" data-id="6e483f3" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-fb3a36e elementor-widget elementor-widget-text-editor" data-id="fb3a36e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>This seems easy but I spent some time googling it on the internet and did not find a straight answer.  I had to move my WordPress site to an AWS LightSail due to some technical issue with my previous web host and I didn&#8217;t know where to put my Ads.txt. </p><p>To cut things short, AWS LightSail&#8217;s Linux + WordPress uses Bitnami and the root directory for its web server is on <b>/OPT/BITNAMI/WORDPRESS.</b></p><p>Just drop your Ads.txt in that directory and you should be good. Once you have uploaded your Ads.txt, you should be able to access it using a browser by going to <b>www.{yourdomain.com}/ads.txt</b>.</p><p>If you are using a different setup or prefer not to deal with technicalities, you can use a WordPress plugin. Login to your WordPress site and add the &#8216;<a href="https://wordpress.org/plugins/wp-file-manager/" target="_blank" rel="noopener">File Manager</a>&#8216; plugin. This plugin will show you the root directory of your WordPress site and from there you can upload to it directly.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-308e326 elementor-widget elementor-widget-template" data-id="308e326" data-element_type="widget" data-widget_type="template.default">
				<div class="elementor-widget-container">
							<div class="elementor-template">
					<div data-elementor-type="section" data-elementor-id="311" class="elementor elementor-311" data-elementor-post-type="elementor_library">
					<section class="elementor-section elementor-top-section elementor-element elementor-element-1c9461bb elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="1c9461bb" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3c156f23" data-id="3c156f23" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-4ad0f189 elementor-widget elementor-widget-html" data-id="4ad0f189" data-element_type="widget" data-widget_type="html.default">
				<div class="elementor-widget-container">
					<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Single-row Horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-7317245021228731"
     data-ad-slot="2931534497"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>				</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				</div>
				</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-7fc21f9 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="7fc21f9" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b756090" data-id="b756090" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-56a9374 elementor-widget elementor-widget-spacer" data-id="56a9374" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				</div>
		<p>The post <a href="https://www.caskispace.com/upload-ads-txt-to-bitnami-web-server/">Upload Ads.txt to Bitnami Web Server</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Query Database Using WordPress REST</title>
		<link>https://www.caskispace.com/how-to-query-wp-database-using-wp-rest/</link>
		
		<dc:creator><![CDATA[saborot]]></dc:creator>
		<pubDate>Sat, 05 Dec 2020 16:22:52 +0000</pubDate>
				<category><![CDATA[Code Crumbs]]></category>
		<guid isPermaLink="false">https://caskispace.com/?p=2893</guid>

					<description><![CDATA[<p>This code crumb will query a WordPress database using WP REST. It tackles all the basic steps to interact and get data from the WordPress database in three different ways; directly from the browser using a URL, calling from a PHP function or by a user-driven event from the front-end interface using Javascript which will [&#8230;]</p>
<p>The post <a href="https://www.caskispace.com/how-to-query-wp-database-using-wp-rest/">Query Database Using WordPress REST</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></description>
										<content:encoded><![CDATA[		<div data-elementor-type="wp-post" data-elementor-id="2893" class="elementor elementor-2893" data-elementor-post-type="post">
						<section class="elementor-section elementor-top-section elementor-element elementor-element-3099518 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="3099518" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-6e483f3" data-id="6e483f3" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-fb3a36e elementor-widget elementor-widget-text-editor" data-id="fb3a36e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>This code crumb will query a WordPress database using WP REST. It tackles all the basic steps to interact and get data from the WordPress database in three different ways; directly from the browser using a URL, calling from a PHP function or by a user-driven event from the front-end interface using Javascript which will then call a PHP function which in turn will query the database and return the result.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-308e326 elementor-widget elementor-widget-template" data-id="308e326" data-element_type="widget" data-widget_type="template.default">
				<div class="elementor-widget-container">
							<div class="elementor-template">
					<div data-elementor-type="section" data-elementor-id="311" class="elementor elementor-311" data-elementor-post-type="elementor_library">
					<section class="elementor-section elementor-top-section elementor-element elementor-element-1c9461bb elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="1c9461bb" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3c156f23" data-id="3c156f23" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-4ad0f189 elementor-widget elementor-widget-html" data-id="4ad0f189" data-element_type="widget" data-widget_type="html.default">
				<div class="elementor-widget-container">
					<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Single-row Horizontal -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-7317245021228731"
     data-ad-slot="2931534497"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>				</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				</div>
				</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-f44ad9e elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="f44ad9e" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-3355e5e" data-id="3355e5e" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-f0672c9 elementor-widget elementor-widget-accordion" data-id="f0672c9" data-element_type="widget" data-widget_type="accordion.default">
				<div class="elementor-widget-container">
							<div class="elementor-accordion">
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2521" class="elementor-tab-title" data-tab="1" role="button" aria-controls="elementor-tab-content-2521" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 1. Register an Endpoint</a>
					</div>
					<div id="elementor-tab-content-2521" class="elementor-tab-content elementor-clearfix" data-tab="1" role="region" aria-labelledby="elementor-tab-title-2521"><h4>Register an Endpoint</h4>
<div class="instructions">
<ul>
 	<li class="color-blue">Create a function which will contain the route registration.</li>
 	<li class="color-green">Define the endpoint&#8217;s route using <a href="https://developer.wordpress.org/reference/functions/register_rest_route/" target="_blank" rel="noopener">&#8216;register_rest_route&#8217;</a>.</li>
 	<li class="color-orange">Add the function to the <a href="https://developer.wordpress.org/reference/hooks/rest_api_init/" target="_blank" rel="noopener">&#8216;rest_api_init&#8217;</a> hook</li>
</ul>
</div>
<div class="instructions-summary">

<hr />

<div class="code color-blue">function register_route() {</div>
<div class="code color-green">
<pre>   register_rest_route('get-user/v1', '/first/(?P&lt;first>.+)/last/(?P&lt;last>.+)', [
      'methods' =&gt; 'GET',
      'callback' =&gt; 'get_user_callback',
      'permission_callback' =&gt; '__return_true',
   ]);
</pre>
</div>
<div class="code color-blue">}</div>
<div class="code color-orange">
<pre>add_action('rest_api_init', 'register_route');</pre>
</div>
</div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2522" class="elementor-tab-title" data-tab="2" role="button" aria-controls="elementor-tab-content-2522" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 2. Create a Callback Function to Query the Database</a>
					</div>
					<div id="elementor-tab-content-2522" class="elementor-tab-content elementor-clearfix" data-tab="2" role="region" aria-labelledby="elementor-tab-title-2522"><h4>Step 2. Create the Callback Function that Will Query the Database</h4>
<div class="instructions">
<ul>
 	<li class="color-blue">Create the callback function for the endpoint</li>
 	<li class="color-green">Get reference to the WordPress Database Access object (<a href="https://developer.wordpress.org/reference/classes/wpdb/" target="_blank" rel="noopener">wpdb</a>).</li>
 	<li class="color-orange">Get the parameters passed to the endpoint.</li>
 	<li class="color-purple">Prepare the database query using <a href="https://developer.wordpress.org/reference/classes/wpdb/prepare/" target="_blank" rel="noopener">wpdb-&gt;prepare</a>. Use &#8216;%s&#8217; for string, &#8216;%d&#8217; for integer and &#8216;%f&#8217; for float values.</li>
 	<li class="color-brown">Query the database and get results. Use <a href="https://developer.wordpress.org/reference/classes/wpdb/get_results/" target="_blank" rel="noopener">wpdb-&gt;get_results</a> for SELECT statements and <a href="https://developer.wordpress.org/reference/classes/wpdb/query/" target="_blank" rel="noopener">wpdb-&gt;query</a> for DELETE/UPDATE.</li>
 	<li class="color-teal">Return the data in a <a href="https://developer.wordpress.org/reference/classes/wp_rest_response/" target="_blank" rel="noopener">WP_REST_Response</a> object.</li>
</ul>
</div>
<div class="instructions-summary">

<hr />

<div class="code color-blue">function get_user_callback(WP_REST_Request $request) {</div>
<div class="code color-green">
<pre>   global $wpdb;
</pre>
</div>
<div class="code color-orange">
<pre>   $first_name = urldecode($request-&gt;get_param( 'first' ));
   $last_name = urldecode($request-&gt;get_param( 'last' ));
</pre>
</div>
<div class="code color-purple">
<pre>   $query = "SELECT * FROM 'users_table' WHERE 'first_name' = %s AND 'last_name' = %s";
   $prepared_query = $wpdb-&gt;prepare($query, $first_name, $last_name);
</pre>
</div>
<div class="code row1 color-brown">
<pre>   $data = $wpdb-&gt;get_results($prepared_query);
</pre>
</div>
<div class="code color-teal">
<pre>   return new WP_REST_Response($data);
</pre>
</div>
<div class="code color-blue">}</div>
</div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2523" class="elementor-tab-title" data-tab="3" role="button" aria-controls="elementor-tab-content-2523" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 3.a Call Directly From a Browser using a URL</a>
					</div>
					<div id="elementor-tab-content-2523" class="elementor-tab-content elementor-clearfix" data-tab="3" role="region" aria-labelledby="elementor-tab-title-2523"><h4>Call Call Directly From a Browser using a URL</h4><div class="instructions"><ul><li class="color-blue">Call directly from a browser by prefixing the Route with <small>/wp-json/</small>.</li></ul></div><div class="instructions-summary"><hr /><p>With parameters: (first name = &#8216;Gieon Felice&#8217; and last name = &#8216;Aborot&#8217;)</p><div class="code color-blue"><br />https://yourdomain.com/wp-json/get-user/v1/first/Gieon Felice/last/Aborot</div></div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2524" class="elementor-tab-title" data-tab="4" role="button" aria-controls="elementor-tab-content-2524" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 3.b Call the Endpoint Using PHP</a>
					</div>
					<div id="elementor-tab-content-2524" class="elementor-tab-content elementor-clearfix" data-tab="4" role="region" aria-labelledby="elementor-tab-title-2524"><h4>Call the Endpoint From PHP</h4><div class="instructions"><ul><li class="color-blue">Use <a href="https://developer.wordpress.org/reference/functions/wp_remote_get/" target="_blank" rel="noopener">wp_remote_get</a> to request data from the endpoint.</li><li class="color-green">Handle errors and unsuccessful requests using <a href="https://developer.wordpress.org/reference/functions/is_wp_error/" target="_blank" rel="noopener">is_wp_error</a> and <a href="https://www.php.net/manual/en/reserved.variables.server.php" target="_blank" rel="noopener">WP_Error</a>.</li><li class="color-orange">Finally use <a href="https://developer.wordpress.org/reference/functions/wp_remote_retrieve_body/" target="_blank" rel="noopener">wp_remote_retrieve_body</a> to get the response.</li></ul></div><div class="instructions-summary"><hr /><p>function call_from_php() {</p><div class="code color-blue"><pre>   $response = wp_remote_get(
      "https://yourdomain.com/wp-json/get-user/v1/first/Gieon Felice/last/Aborot", [
         'sslverify' =&gt; false,
      ]
   );
</pre></div><div class="code color-green"><pre>   if ( is_wp_error( $response ) ) {
      return new WP_Error( 'call_from_php unsuccessful', $response-&gt;get_error_message() );
   }
</pre></div><div class="code color-orange"><pre>   return wp_remote_retrieve_body($response);
</pre></div><p>}</p></div></div>
				</div>
							<div class="elementor-accordion-item">
					<div id="elementor-tab-title-2525" class="elementor-tab-title" data-tab="5" role="button" aria-controls="elementor-tab-content-2525" aria-expanded="false">
												<a class="elementor-accordion-title" tabindex="0">Step 3.c Trigger the Call to the Endpoint From the Front-end Using Javascript</a>
					</div>
					<div id="elementor-tab-content-2525" class="elementor-tab-content elementor-clearfix" data-tab="5" role="region" aria-labelledby="elementor-tab-title-2525"><h4>Trigger the Call to the Endpoint From the Front-end Using Javascript</h4><div class="instructions"><ul><li class="color-blue">In PHP, declare the ajax_url object using <a href="https://developer.wordpress.org/reference/functions/wp_localize_script/" target="_blank" rel="noopener">wp_localize_script</a>. This is needed to call the PHP function from JS using the AJAX request.</li><li class="color-green">Add wordpress hooks to make a connection between the AJAX request action and the name of php function to call. &#8216;wp_ajax_nopriv&#8217; fires ONLY if the user is not logged-in while &#8216;wp_ajax&#8217; fires ONLY if the user is logged-in.</li><li class="color-orange">Create a PHP function which will be called from Javascript and in turn will call the Endpoint.</li><li class="color-purple">Finally in Javascript, create the function that will make an AJAX request to call the PHP function.</li></ul></div><div class="instructions-summary"><hr /><div class="code color-blue"><pre>wp_localize_script(
   'id_on_the_enqueued_script_that_will_use_it',
   'my_ajax_object',
   [ 'ajax_url' =&gt; admin_url( 'admin-ajax.php' ) ]
);
</pre></div><hr /><div class="code color-green"><pre>add_action('wp_ajax_nopriv_call_from_js_to_php', 'call_from_php');
add_action('wp_ajax_call_from_js_to_php', 'call_from_php');
</pre></div><hr /><div class="code color-orange"><pre>function call_from_php() {

   $first_name = $_GET['first_name'];
   $last_name = $_GET['last_name'];

   $response = wp_remote_get(
      "https://yourdomain.com/wp-json/get-user/v1/first/$first_name/last/$last_name", [
         'sslverify' =&gt; false,
      ]
   );

   if ( is_wp_error( $response ) ) {
      return new WP_Error( 'call_from_php unsuccessful', $response-&gt;get_error_message() );
   }

   wp_send_json(wp_remote_retrieve_body($response), 200);
}
</pre></div><hr /><div class="code color-purple"><pre>function call_from_js() {
   jQuery.ajax(my_ajax_object.ajax_url, {
      type: 'GET',
      data: {
            action: 'call_from_js_to_php',
            first_name: 'Gieon Felice',
            last_name: 'Aborot',
      },
   }).done(response =&gt; {
      const parsedResults = JSON.parse(response);
      // Do something with the result
   }).fail(error =&gt; {
      // Do something when an unsuccessful
   });
}
</pre></div></div></div>
				</div>
								</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-7fc21f9 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="7fc21f9" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-b756090" data-id="b756090" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-56a9374 elementor-widget elementor-widget-spacer" data-id="56a9374" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				</div>
		<p>The post <a href="https://www.caskispace.com/how-to-query-wp-database-using-wp-rest/">Query Database Using WordPress REST</a> appeared first on <a href="https://www.caskispace.com">caskiSPACE</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
