How to track the movement of the user between the two domains?
scheme:
1) the user clicks the button in doman-exemple1.com and go out from doman-exemple1.com and comes to doman-exemple2.com
login
2) doman-exemple2.com executed only on the server (that is not possible to use javascript)
<?php
// executed something codes... ;
// go with result to doman-exemple1.com
header('Location: http://doman-exemple1.com/?good='.$code);
?>
3) user returned to the http://doman-exemple1.com/?good=t45ygsw45t4
I need that google analytics understand that this same user and it not different users
This works:
https://support.google.com/analytics/answer/1034342?hl=en
But I cannot use thit becouse doman-exemple2.com executed only PHP and cannot use javascript
please help how to do this without javascript and only with PHP
P.S.
I read this: https://github.com/thomasbachem/php-ga but not understand if i can use this in my situations
If you are using Universal Analytics of GA, then have a look at the Measurement Protocol.
https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
That can allow you to track through PHP only, and by using the same cid you can continue to track (using the same profile) the session.
thenx for Marcel Dumont
code get from this example: http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/
doman-exemple1.com:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
//https://support.google.com/analytics/answer/1034342?hl=uk
//https://developers.google.com/analytics/devguides/collection/analyticsjs/cross-domain
ga('create', 'UA-33396333-20', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['doman-exemple2.com'] );
ga('send', 'pageview');
</script>
doman-exemple2.com:
<?php
if (isset($_GET["_ga"])) {
setcookie('_ga_my', $_GET["_ga"],time()+3600*24*24);
list($version,$domainDepth, $cid1, $cid2) = split('[\.]', $_GET["_ga"],4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
$cid = $contents['cid'];
$ga = "GA1.2.".$cid;
setcookie('_ga', $ga,time()+3600*24*24);
}
function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version,$domainDepth, $cid1, $cid2) = split('[\.]', $_COOKIE["_ga"],4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
$cid = $contents['cid'];
}
else $cid = gaGenUUID();
return $cid;
}
// Generate UUID v4 function - needed to generate a CID when one isn't available
function gaGenUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
//echo gaParseCookie();
function gaBuildHit( $method = null, $info = null ) {
if ( $method && $info) {
// Standard params
$v = 1;
$tid = "UA-33396333-20"; // Put your own Analytics ID in here
$cid = gaParseCookie();
// Register a PAGEVIEW
if ($method === 'pageview') {
// Send PageView hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'pageview',
'dt' => $info['title'],
'dp' => $info['slug']
);
gaFireHit($data);
} // end pageview method
}
}
// See https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
function gaFireHit( $data = null ) {
if ( $data ) {
$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$result = file_get_contents( $getString );
#$sendlog = error_log($getString, 1, "ME#EMAIL.COM"); // comment this in and change your email to get an log sent to your email
return $result;
}
return false;
}
$data = array(
'title' => 'Login doman-exemple2.com',
'slug' => 'doman-exemple2.com'
);
gaBuildHit( 'pageview', $data);
$secrret="111";
if (isset($_COOKIE['_ga_my'])) {
$gogl='&_ga='.$_COOKIE['_ga_my'];
header('Location: https://doman-exemple1.com/mass-add-email?utm_source=good&secret='.$secrret.'&utm_medium=good&utm_campaign=good'.$gogl);
} else {
header('Location: https://doman-exemple1.com/mass-add-email?utm_source=good&secret='.$secrret.'&utm_medium=good&utm_campaign=good');
}
exit();
?>
Related
I want to integrate the payment partal without displaying the MasterCard interface to user the will only fill their information on my web
here
my portal
and not here
mastercard portal
I am trying to implement CommWeb on my PHP site using curl. It isn't working. I am getting following error message:
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (!isset($_GET['id']) || !isset($_GET['motif']) || !isset($_GET['code'])){
die("Invalid request");
}
$amount = 10000;
$id = 2;
$motif = 'save';
$action_code = 'ookkk';
$orderInfo = 'fhhsd'.$id;
$MerchTxnRef = $orderInfo.'-'.generateMerchTxnRef();
$accountData = array(
'merchant_id' => 'TESTID', // for test card
'access_code' => '77867878', // for test card
'secret' => 'TYUJHGFDFGHJ87654567GFDFGHGF' // for test card
);
$currency_str = "USD";
$mult = 100;
$queryData = array(
'vpc_AccessCode' => $accountData['access_code'],
'vpc_Merchant' => $accountData['merchant_id'],
'vpc_Amount' => 1000000, // Multiplying by 100 to convert to the smallest unit
'vpc_OrderInfo' => $orderInfo,
'vpc_MerchTxnRef' => $MerchTxnRef,
'vpc_Command' => 'pay',
'vpc_Currency' => $currency_str,
'vpc_Locale' => 'en',
'vpc_Version' => 2,
'vpc_ReturnURL' => ('http://theeventsfactory.biz/the_events_factory/logics/payment_return.php?id='.$id.'&motif='.$motif.'&code='.$action_code),
'vpc_SecureHashType' => 'SHA256',
'vpc_CardNum' => '5123456789012346',
'vpc_CardExp' => '0521',
'vpc_CardSecurityCode'=> '123'
);
// Add secure secret after hashing
// $queryData['vpc_SecureHash'] = generateSecureHash($accountData['secret'], $queryData);
// $migsUrl = 'https://migs.mastercard.com.au/vpcpay?'.http_build_query($queryData);
$ch = curl_init("https://migs.mastercard.com.au/vpcdps");
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $queryData,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false
));
$response = curl_exec($ch);
print_r($response);
}
function generateMerchTxnRef() {
$txnRef = rand(9, 9999999999999999);
// Saved in the database associated with the order id
return $txnRef;
}
function generateSecureHash($secret, array $params) {
$secureHash = "";
// Sorting params first based on the keys
ksort($params);
foreach ($params as $key => $value)
{
// Check if key equals to vpc_SecureHash or vpc_SecureHashType to discard it
if(in_array($key, array('vpc_SecureHash', 'vpc_SecureHashType'))) continue;
// If key either starts with vpc_ or user_
if(substr( $key, 0, 4 ) === "vpc_" || substr($key, 0, 5) === "user_") {
$secureHash .= $key."=".$value."&";
}
}
// Remove the last `&` character from string
$secureHash = rtrim($secureHash, "&");
//
return strtoupper(hash_hmac('sha256', $secureHash, pack('H*', $secret)));
}
This is the response i am receiving
vpc_Amount=0&vpc_BatchNo=0&vpc_Locale=en&vpc_Message=Required+field+vpc_Merchant+was+not+present+in+the+request&vpc_TransactionNo=0&vpc_TxnResponseCode=7
How will i solve this problem
You need to convert the array to a url encoded string with http_build_query
after setting the queryData array, add
$queryData_string = http_build_query($queryData);
Then the curlpost would be set to that
CURLOPT_POSTFIELDS => $queryData_string,
I have been struggling to implement Google's Measurement Protocol on a Wordpress site.
Apache Server, using PHP 5.4.35.
What I want to do is get server-side visitor data loaded into Google Analytics. The site has huge discrepancies between server logs and GA data, and I'm hoping to get the two to (somewhat) match up.
I have taken the code published by Stu Miller on his website, and made some slight edits hoping to reflect my use case. I've added the following to my wp-includes/functions.php file:
require( ABSPATH . WPINC . '/http.php' );
require( ABSPATH . WPINC . '/class-http.php' );
require( ABSPATH . WPINC . '/general-template.php' );
// Handle the parsing of the _ga cookie or setting it to a unique identifier
// Generate UUID v4 function - needed to generate a CID when one isn't available
function gaGenUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version,$domainDepth, $cid1, $cid2) = split('[\.]', $_COOKIE["_ga"],4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
$cid = $contents['cid'];
}
else $cid = gaGenUUID();
return $cid;
}
function gaBuildHit( $method = null, $info = null ) {
if ( $method && $info) {
// Standard params
$v = 1;
$tid = "UA-xxxxxxx"; // Put your own Analytics ID in here
$cid = gaParseCookie();
// Register a PAGEVIEW
if ($method === 'pageview') {
// Send PageView hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'pageview',
'dt' => $info['title'],
'dp' => $info['slug']
);
gaFireHit($data);
} // end pageview method
// Register an ECOMMERCE TRANSACTION (and an associated ITEM)
else if ($method === 'ecommerce') {
// Set up Transaction params
$ti = uniqid(); // Transaction ID
$ta = 'SI';
$tr = $info['price']; // transaction value (native currency)
$cu = $info['cc']; // currency code
// Send Transaction hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'transaction',
'ti' => $ti,
'ta' => $ta,
'tr' => $tr,
'cu' => $cu
);
gaFireHit($data);
// Set up Item params
$in = urlencode($info['info']->product_name); // item name;
$ip = $tr;
$iq = 1;
$ic = urlencode($info['info']->product_id); // item SKU
$iv = urlencode('SI'); // Product Category - we use 'SI' in all cases, you may not want to
// Send Item hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
't' => 'item',
'ti' => $ti,
'in' => $in,
'ip' => $ip,
'iq' => $iq,
'ic' => $ic,
'iv' => $iv,
'cu' => $cu
);
gaFireHit($data);
} // end ecommerce method
}
}
global $post;
$data = array(
// Get the queried object and sanitize it
'title' => $title = $post->post_title,
'slug' => $slug = $post->post_name
);
gaBuildHit( 'pageview', $data);
// See https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
function gaFireHit( $data = null ) {
if ( $data ) {
$getString = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$result = wp_remote_get( $getString );
#$sendlog = error_log($getString, 1, "ME#EMAIL.COM"); // comment this in and change your email to get an log sent to your email
return $result;
}
return false;
}
I have received several errors along the way, which started with the wp_remote_get() function being not defined, which resulted in my including the:
require( ABSPATH . WPINC . '/http.php' );
This inclusion moved up the error list to WP_Http() class not found, which resulted in my inclusion of:
require( ABSPATH . WPINC . '/class-http.php' );
... and so on. I don't quite remember the error which prompted my inclusion of:
require( ABSPATH . WPINC . '/general-template.php' );
I believe it was a PHP Fatal error: Call to undefined function get_bloginfo().
Anyway, I think I'm doing includes wrong. The latest fatal error is: Fatal error: Call to undefined function home_url() in /home/user/public_html/domain/wp-includes/general-template.php on line 658
I looked up the Wordpress Codex a bit and it also seems like the wp_remote_safe_post() would have been the better option, with Google recommending POST instead of GET due to the restricted payload on GET.
Unfortunately I lack the experience to make sense of how the code is supposed to be used, I had been hoping the initial code would be good enough to make happen, with maybe some tweaks.
Am I doing something wrong with the includes? Or has wordpress 4.6.1 moved so far ahead that all of this is deprecated? Or maybe I'm using the wrong functions.php file?
I am attempting to construct a postcode checker for a small number of stores in Australia. Basically, the customer enters their zip code and the site will then redirect to appropriate local pricing/services.
I am using this as a base but I am having a problem (listed by one of the comments on that site too) that no matter what value is entered for some reason the code marks the zip as SA even when it is completely different.
The code I am using is below.
<?php
// v1.0 Postcode Checker Just iStuff
// Copyright Ben Green 2014
// Redirect Customers to Appropriate Local Pricing and Services
// Sets cookie upon entering postcode, will then remember postcode for 3 days on customer's system
if (preg_match('#^\d+$#', $_POST['cf_postcode'])):
else:
print "Please enter your postcode correctly.";
endif;
$postcode = $_POST['cf_postcode'];
function findState($postcode) {
$ranges = array(
'NSW' => array(
1000, 1999,
2000, 2599,
2619, 2898,
2921, 2999
),
'ACT' => array(
200, 299,
2600, 2618,
2900, 2920
),
'VIC' => array(
3000, 3999,
8000, 8999
),
'QLD' => array(
4000, 4999,
9000, 9999
),
'SA' => array(
5000, 5999
),
'WA' => array(
6000, 6797,
6800, 6999
),
'TAS' => array(
7000, 7999
),
'NT' => array(
800, 999
)
);
$exceptions = array(
0800 => 'NT',
872 => 'NT',
2540 => 'NSW',
2611 => 'ACT',
2620 => 'NSW',
3500 => 'VIC',
3585 => 'VIC',
3586 => 'VIC',
3644 => 'VIC',
3707 => 'VIC',
2899 => 'NSW',
6798 => 'WA',
6799 => 'WA',
7151 => 'TAS'
);
$postcode = intval($postcode);
if ( array_key_exists($postcode, $exceptions) ) {
return $exceptions[$postcode];
}
foreach ($ranges as $state => $range)
{
$c = count($range);
for ($i = 0; $i < $c; $i+=2) {
$min = $range[$i];
$max = $range[$i+1];
if ( $postcode >= $min && $postcode <= $max ) {
return $state;
}
}
}
return null;
}
//Redirect for NT
if ($state = NT)
{
header( "Location: http://www.justistuff.com.au/ntrepairs.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for QLD
if ($state = QLD)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for VIC
if ($state = VIC)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for ACT
if ($state = ACT)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for NSW
if ($state = NSW)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for WA
if ($state = WA)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for TAS
if ($state = TAS)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for SA
if ($state = SA)
{
header( "Location: http://www.justistuff.com.au/sarepairs.php" );
setcookie("postcode", $postcode, time()+259200);
}
I can't seem to work out what is causing it to redirect to SA even when the value entered (for example 2142) is clearly marked as NSW
if ($state = VIC)
That has two issues. First of all = is used for assignment, not for comparison. Secondly VIC is a string and should be in quotes like
if ($state == 'VIC')
Fix that for all your if statements and you're good.
Also, you are not calling findState function anywhere, before you start comparing the list of states you need to call that function to see what state your postcode belongs to
$state=findState($postcode);
//if($state=="thisthat")
it is classic == problem, in your if you have assignment operator used instead of equality operator.
Also, I suggest using exit whenever you use header redirect, it ensure the next line of code didn't get executed.
To explain the problem from your partial code. you are using if statement to compare and then instead of equality you are assigning the value to $state. this result in true for if and hence the code instead if block will get executed. Since you don't have exit it parse next if.
I suggest changing your if to switch with break in there.
I created this piece of code to use Google Analytics Measurement Protocol instead of the google provided javascript, analytics.js in one of my page.
Recently I was playing with analytics javascript code, and I found that many times it will not collect data if something is blocking tracking codes (eg.: analytics.js) like when PrivDog is installed (amongs others).
The original idea is coming from here, but this is for wordpress.
http://www.stumiller.me/implementing-google-analytics-measurement-protocol-in-php-and-wordpress/
I modified the code to send data with sockets. (and without the wordpress functions library)
$gclid = $_GET['gclid'];
if (!empty($gclid)) {
setcookie("gclid", $gclid, time() + (10 * 365 * 24 * 60 * 60));
}
// Handle the parsing of the _ga cookie or setting it to a unique identifier
function gaParseCookie()
{
if (isset($_COOKIE['_ga'])) {
list($version, $domainDepth, $cid1, $cid2) = split('[\.]', $_COOKIE["_ga"], 4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1 . '.' . $cid2);
$cid = $contents['cid'];
} else {
$cid = gaGenUUID();
}
return $cid;
}
// Generate UUID v4 function - needed to generate a CID when one isn't available
function gaGenUUID()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
function gaBuildHit($method = null, $info = null)
{
global $gclid;
if ($method && $info) {
// Standard params
$v = 1;
$tid = "UA-XXXXXXX-XX"; // Put your own Analytics ID in here
$cid = gaParseCookie();
if (empty($gclid)) {
$gclid = $_COOKIE['gclid'];
}
// Register a PAGEVIEW
if ($method === 'pageview') {
// Send PageView hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
'gclid' => $gclid,
't' => 'pageview',
'dt' => $info['title'],
'dp' => $info['slug']
);
gaFireHit($data);
} // end pageview method
//
// Register an ECOMMERCE TRANSACTION (and an associated ITEM)
else if ($method === 'ecommerce') {
// Send Transaction hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
'gclid' => $gclid,
't' => 'transaction',
'ti' => $info['tid'], // Transaction ID
'ta' => 'SI',
'tr' => $info['price'], // transaction value (native currency)
'ts' => $info['shipping'], // transaction shipping (native currency)
'cu' => $info['cc'] // currency code
);
gaFireHit($data);
// Set up Item params
if ($info['info']->product_name) {
$in = urlencode($info['info']->product_name); // item name;
$ip = $tr;
$iq = 1;
$ic = urlencode($info['info']->product_id); // item SKU
$iv = urlencode('SI'); // Product Category - we use 'SI' in all cases, you may not want to
// Send Item hit
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
'gclid' => $gclid,
't' => 'item',
'ti' => $ti,
'in' => $in,
'ip' => $ip,
'iq' => $iq,
'ic' => $ic,
'iv' => $iv,
'cu' => $cu
);
gaFireHit($data);
}
} else if ($method === 'event') {
$data = array(
'v' => $v,
'tid' => $tid,
'cid' => $cid,
'gclid' => $gclid,
'ec' => $info['category'],
'ea' => $info['event'],
'el' => $info['label'],
'ev' => $info['value']
);
gaFireHit($data);
}
}
}
// See ttps://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
function gaFireHit($data = null)
{
if ($data) {
$url = 'https://ssl.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$result = socketPost($url, $getString, $_SERVER['HTTP_USER_AGENT']);
return $result;
}
return false;
}
function socketPost($url, $post_string, $ua = null)
{
$parts = parse_url($url);
// workout port and open socket
$port = isset($parts['port']) ? $parts['port'] : 80;
$success = $fp = fsockopen($parts['host'], $port, $errno, $errstr, 30);
if ($fp) {
// create output string
$output = "POST " . $parts['path'] . " HTTP/1.1\r\n";
if (is_string($ua)) {
$output .= "User-Agent: " . $ua . "\r\n";
}
$output .= "Host: " . $parts['host'] . "\r\n";
$output .= "Content-Type: application/x-www-form-urlencoded\r\n";
$output .= "Content-Length: " . strlen($post_string) . "\r\n";
$output .= "Connection: Close\r\n\r\n";
$output .= isset($post_string) ? $post_string : '';
// send output to $url handle
$success = fwrite($fp, $output);
fclose($fp);
}
return $success ? true : false;
}
//examples:
//page hit
$data = array(
'title' => 'Page title',
'slug' => $_SERVER["REQUEST_URI"]
);
gaBuildHit('pageview', $data);
//ecommerce
$data = array(
'tid' => $order_id_in_merchant_system,
'cc' => 'AUD', // the currency code
'price' => $order_total, // the price
'shipping' => $order_shipping, // the shipping cost
'country' => 'AU' // the country ISO code
);
gaBuildHit('ecommerce', $data);
The code itself is working just fine. I put here for you to get the idea.
Question is, I dont really see the flow, how could an Adwords Click get tracked here. Like, if there is no _ga cookie set before (then gaParseCookie creates one), how could analytics connect the tracked event into eg.: an Analytics keyword the click coming from? Is that possible with Measurement Protocol?
I have edited my original question because I think made it more complicated than what I need.
Here is what I need to add:
Check if the input for $query has the word hockey in it
if not append hockey to the string
return $query
Here is the portion of the php code that I need to adjust:
public function search($query, $size = 10, $page = 0, $location = '', $miles = 5, $sort = 'rd') {
$params = array(
'q' => rawurlencode(trim($query)),
'sb' => $sort,
'ws' => $size,
'pn' => (intval($page) < 1 ? 0 : intval($page)),
);
You don't need a regex for this; strpos will be faster anyway. Note the triple === sign. (manual)
public function search($query, $size = 10, $page = 0, $location = '', $miles = 5, $sort = 'rd') {
if( strpos( $query, 'hockey' ) === false ) {
$query .= ' hockey';
}
$params = array(
'q' => rawurlencode(trim($query)),
'sb' => $sort,
'ws' => $size,
'pn' => (intval($page) < 1 ? 0 : intval($page)),
);
Use strpos to find the first ocurrence of hockey in the queried string
http://es1.php.net/manual/en/function.strpos.php
The function strpos returns false if the substring (in this case, hockey is not found).
So you can do something like:
if !strpos($query, 'hockey') {
$query = $query . ' hockey';
}