Last active
April 22, 2024 15:17
-
-
Save karptonite/408f7b5597a21af08a7a68f23f512deb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This code is not intended to be copied to your system. Rather, you should adapt something like this to | |
// your store, then use a cron job to run it periodically; we recommend once an hour. | |
$query = "SELECT bgg_objectid, url, price FROM products WHERE bgg_objectid IS NOT NULL ORDER BY bgg_objectid"; | |
$db = new GeekDB(); | |
$db->setQuery( $query ); | |
$results = $db->fetchAllAssoc(); | |
$filename = tempnam( '/tmp', 'bggstore_' ); | |
$fp = fopen( $filename, 'w' ); | |
fputcsv( $fp, ['objectid', 'url', 'price', 'currency', 'enabled', 'show_from'] ); | |
// how affiliate tags are added will vary depending on how your affiliate system works | |
foreach ( $results as $result ) { | |
// Adding the affiliate tag | |
$href = $result['href'] . '?aff=10'; | |
fputcsv( $fp, [ | |
$result['bgg_objectid'], | |
$href, | |
$price, | |
'USD', | |
1, | |
0 | |
] ); | |
} | |
fclose( $fp ); | |
$domain = 'boardgamegeek.com'; | |
try { | |
$guzzle = new GuzzleHttp\Client( ['cookies' => true] ); | |
// Log in. Because guzzle is using cookies, we will be logged in for the following request | |
$res = $guzzle->post( 'https://' . $domain . '/login/api/v1', [ | |
'json' => [ 'credentials' => [ | |
'username' => $GLOBALS['geekconfig']['boardgamegeek_account_credentials']['username'], | |
'password' => $GLOBALS['geekconfig']['boardgamegeek_account_credentials']['password'], | |
] ], | |
] ); | |
// direct upload from the file | |
$handle = fopen( $filename, 'r' ); | |
echo "sending request"; | |
$res = $guzzle->request( 'POST', 'https://' . $domain . '/geekaffiliate.php', [ | |
'multipart' => [ | |
[ 'name' => 'action', 'contents' => 'bulkupload'], | |
[ | |
'name' => 'filename', | |
'contents' => $handle | |
]] | |
] ); | |
// For uploading from a file url | |
// $res = $guzzle->request( 'POST', 'https://boardgamegeek.com/geekaffiliate.php', [ | |
// 'form_params' => [ 'action' => 'bulkupload', 'urlfilename' => $url ] | |
// ] ); | |
} catch ( Exception $e ) { | |
echo $e->getMessage(); | |
} | |
fclose( $handle ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment