Curl Exception 7 PHP and Guzzle with Elasticsearch - php

I am trying to index documents using the php client for elastic search which uses Guzzle. After compiling my php script I am getting an error that says Internal Server Error, code 500. After doing some research this seems to be an issue with connecting to a server but the strange part is that everything I'm trying to do is set up on the same machine. My instance of Elasticsearch, my documents I'm trying to index, and my php scripts are all saved and running on the same machine. This is my PHP Script:
<?php
require '/home/aharmon/vendor/autoload.php';
$client = new Elasticsearch\Client();
$root = realpath('/home/aharmon/elkdata/for_elk_test_2014_11_24/Agencies');
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD);
$paths = array($root);
foreach ($iter as $path => $dir) {
if ($dir -> isDir()) {
$paths[] = $path;
}
}
//Create the index and mappings
$mapping['index'] = 'rvuehistoricaldocuments2009-2013'; //mapping code
$mapping['body'] = array (
'mappings' => array (
'documents' => array (
'_source' => array (
'enabled' => true
),
'properties' => array(
'doc_name' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'description' => array(
'type' => 'string'
)
)
)
)
);
//Now index the documents
for ($i = 0; $i <= 10000; $i++) {
$params ['body'] [] = array(
'index' => array(
'_id' => $i
)
);
$params ['body'] [] = array(
'type' => 'documents',
'body' => array(
'foo' => 'bar'//Document body goes here
)
);
//Every 1000 documents stop and send the bulk request.
if($i % 1000) {
$responses = $client->bulk($params);
// erase the old bulk request
$params = array();
// unset the bulk response when you are done to save memory
unset($responses);
}
}
$client ->indices()->create($mapping)
?>
If anyone has seen this before or has an inclination as to what the issue the help would be greatly appreciated. I had a similar issue before when I tried to set up SSH but I got the firewall all configured and got SSH working so I'm not sure why this is happening.

check this link it is ok for me :
http://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_index_operations.html#_put_mappings_api
<?php
// Set the index and type
$params['index'] = 'my_index';
$params['type'] = 'my_type2';
// Adding a new type to an existing index
$myTypeMapping2 = array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'first_name' => array(
'type' => 'string',
'analyzer' => 'standard'
),
'age' => array(
'type' => 'integer'
)
)
);
$params['body']['my_type2'] = $myTypeMapping2;
// Update the index mapping
$client->indices()->putMapping($params);

Related

authentication to MQ via mqseries library php

I need some help with php mqseries library.
I have some troubles connecting to Queue-manager. It does connect without authentication, but when I'm trying to use MQCSP, I get
2035 error.
I've contacted the developers, one of them told me that he no longer works on it, others don't respond.
It looks like on IBM it doesn't work.
Here is my connection code:
$cfg = array();
$cfg['ESB_ADDRESS'] = '10.4.116.110(1416)';
$cfg['ESB_CHANNEL'] = 'SITEEXT.SVRCONN';
$cfg['ESB_QUEUE_MANAGER'] = 'IIB.ADP.MI1';
$cfg['ESB_QUEUE_NAME'] = 'SITEEXT_TO_ESB';
$cfg['ESB_TOPIC_STRING'] = '';
$cfg['USERID'] = 'svcgo-site';
$cfg['PASSWORD'] = 'site91';
$cfg['QMgrName'] = 'IIB.ADP.MI1';
$cfg['DiscInterval'] = '10';
/*
$cfg['ESB_ADDRESS'] = '10.4.111.139(1414)';
$cfg['ESB_CHANNEL'] = 'SITEEXT.SVRCONN';
$cfg['ESB_QUEUE_MANAGER'] = 'QM01';
$cfg['ESB_QUEUE_NAME'] = 'TEST_QUEUE1';
//$cfg['ESB_QUEUE_NAME'] = 'SITEEXT_TO_ESB';
$cfg['ESB_TOPIC_STRING'] = '';
$cfg['USERID'] = 'svcgo-site';
$cfg['PASSWORD'] = 'site91';
//$cfg['QMgrName'] = 'QM01';
$cfg['DiscInterval'] = '10';
*/
$connectionOptions = array(
'StrucId' => MQSERIES_MQCNO_STRUC_ID,
'Version' => MQSERIES_MQCNO_CURRENT_VERSION,
'Options' => MQSERIES_MQCNO_STANDARD_BINDING,
//MQSERIES_USE_MQCSP_AUTHENTICATION,
'MQCD' => array(
//'Version' => MQSERIES_MQCD_VERSION_11,
'ChannelName' => $cfg['ESB_CHANNEL'],
'TransportType' => MQSERIES_MQXPT_TCP,
'ConnectionName' => $cfg['ESB_ADDRESS']
),
MQSERIES_USE_MQCSP_AUTHENTICATION => true,
MQSERIES_MQCSP_AUTHENTICATION => true,
USER_AUTHENTICATION_MQCSP => true,
USE_MQCSP_AUTHENTICATION => true,
MQSERIES_USER_AUTHENTICATION_MQCSP => true,
useMQCSPAuthentication => true,
'MQCSP' => array(
'Version' => MQSERIES_MQCSP_CURRENT_VERSION,
'StrucId' => MQSERIES_MQCSP_STRUC_ID,
'AuthenticationType' => MQSERIES_MQCSP_AUTH_USER_ID_AND_PWD,
'CSPUserIdPtr' => $cfg['USERID'],
'CSPUserIdLength' => strlen($cfg['USERID']),
'CSPPasswordLength' => strlen($cfg['PASSWORD']),
'CSPPasswordPtr' => $cfg['PASSWORD']
),
/*
'ClientConnPtr' => array(
//'Version' => MQSERIES_MQCD_VERSION_11,
'ChannelName' => $cfg['ESB_CHANNEL'],
'TransportType' => MQSERIES_MQXPT_TCP,
'ConnectionName' => $cfg['ESB_ADDRESS']
)
*/
);
mqseries_connx($cfg['ESB_QUEUE_MANAGER'], $connectionOptions, $connection, $completionCode, $reason);
if ($completionCode !== MQSERIES_MQCC_OK) {
die("Connx CompCode : {$completionCode} Reason : {$reason} Text : " . mqseries_strerror($reason));
}
else{echo "Good<br>";}
Php v.5.3.17
IBM MQ 9
Mqseries client library v 0.15.0
That PHP package does not have the code in it to handle the MQCSP structure and add it to the CNO. Looks like you would need to modify the _mqseries_set_mqcno_from_array function and how it is called.

How to use datetimerenderoption in google sheets api v4 in php?

I have to write 3 columns in google sheet using v4 api in php
$range = $sheetName;
$response = $service->spreadsheets_values->get($spreadsheetId, $range);
$values = $response->getValues();
$lastRow = count($values);
$rowIndex = $lastRow;
$columnIndex = 0;
$requests = array();
$requests[] = new Google_Service_Sheets_Request(array(
'updateCells' => array(
'start' => array(
'sheetId' => $sheetId,
'rowIndex' => $rowIndex,
'columnIndex' => $columnIndex
),
'rows' => array(
array(
'values' => array(
array(
'userEnteredValue' => array('numberValue' => '111111'),
'userEnteredFormat' => array('backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8))
),
array(
'userEnteredValue' => array('stringValue' => 'aaaaaaaaa'),
'userEnteredFormat' => array('backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8))
),
array(
'userEnteredValue' => array('numberValue' => '2015-05-05'),
'userEnteredFormat' => array('numberFormat' => array('type'=>'DATE', 'pattern'=>'yyyy-mm-dd'), 'backgroundColor' => array('red'=>1, 'green'=>0.94, 'blue'=>0.8))
)
)
)
),
'fields' => 'userEnteredValue,userEnteredFormat.backgroundColor'
)
));
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(array(
'requests' => $requests
));
try {
$service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
} catch(Exception $e) {
print_R($e);
}
I am using above code to do the task, the only problem I am facing is that I am getting an error while inserting date.
I read that datetimerenderoption is to be used for inserting the date but I am not getting how to do it.
DateTimeRenderOption is used in the values collection APIs (and are also just for reading, not writing). You're using the spreadsheets collection APIs, which are more bare-bones and don't use that convenience option.
If you paste the error you're receiving it will be easier to help, but I'm assuming it has something to do with the lines that set a numberValue. The docs say numberValue takes a number, not a string. If you'd like to set a date, you'll need to provide the number for that date in Serial Number format. You can review this SO answer for good suggestions on dealing with serial numbers.

POST request in PHP is returning PHP code when file extension is used

I am sending post requests in PHP to get a boolean value from my API (so it should return wither true or false)
This is the code I am using in the file for my API. The file is called users.php
if ($_POST['type'] == "authenticateMinecraft"){
$p = new dibdibs\post(
array(
'url' => 'https://authserver.mojang.com/authenticate',
'data' => array(
'agent' => array(
'name' => 'Minecraft',
'version' => 1
),
'username' => $_POST['username'],
'password' => $_POST['password'],
'clientToken' => "33225A179D9A4E1BDA73C012C1C3CBAB8BD00326883BDBEB6FA682482E40F68D"
)
)
);
$res = $p->json();
if (isset($res["selectedProfile"])){
echo("true");
}
else{
echo("false");
}
}
This is the code I am using to reference it (I am using a class which I have put on Pastebin to actually send the request).
$params = array(
'data' => array(
'type' => 'authenticateMinecraft',
'username' => $mcuname,
'password' => $mcpasswd
),
'url' => "api/users.php"
);
$c = new dibdibs\post($params);
$r = $c->http();
var_dump($r);
Whenever I use the .php fule extension when defining url, the whole PHP code of the API page is returned, but when I remove the extension, only true or false is returned. Why is this and is it a problem that I should be aware of and I should fox?

Storing key value dictionary in Elgg

I am working on a plugin for Elgg that keeps track of device ids that are sent to the application when logging in from your mobile phone. For this, I would like to store these device ids in the database and would like to use ElggObjects for this.
This is what I do now:
function initialize() {
$androidTokens = elgg_get_entities(array(
'type' => 'object',
'subtype' => 'androidTokens',
'limit' => 0
));
$iosTokens = elgg_get_entities(array(
'type' => 'object',
'subtype' => 'iosTokens',
'limit' => 0
));
if ($androidTokens == 0) {
$tokenObject = new ElggObject();
$tokenObject->subtype = 'androidTokens';
$tokenObject->tags = array();
$tokenObject->save();
}
if ($iosTokens == 0) {
$tokenObject = new ElggObject();
$tokenObject->subtype = 'iosTokens';
$tokenObject->tags = array();
$tokenObject->save();
}
}
So this generates two ElggObjects that hold ids for android and for ios devices, stored in the metadata field tags. This array of tags can however not be retrieved anymore. When I do:
$tokenObject = elgg_get_entities(array(
'type' => 'object',
'subtype' => $os.'Tokens',
'limit' => 0
));
$tokens = $tokenObject->tags
tokens remains empty. Does someone know what I am doing wrong? Am I using the Elgg objects wrong?
I think the reason you're running into issues there is that elgg_get_entities returns an array of entities.
Am I correct in assuming that you'll only ever have one of each token object subtype? (One for iOS and one for Android?)
If so, I would modify your code as follows:
function initialize() {
$androidTokens = elgg_get_entities(array(
'type' => 'object',
'subtype' => 'androidTokens',
'limit' => 1 // only expecting one entity
));
$iosTokens = elgg_get_entities(array(
'type' => 'object',
'subtype' => 'iosTokens',
'limit' => 1 // only expecting one entity
));
if (count($androidTokens) == 0) {
$tokenObject = new ElggObject();
$tokenObject->subtype = 'androidTokens';
$tokenObject->tags = array();
$tokenObject->save();
}
if (count($iosTokens) == 0) {
$tokenObject = new ElggObject();
$tokenObject->subtype = 'iosTokens';
$tokenObject->tags = array();
$tokenObject->save();
}
}
Later, when grabbing the entity:
$tokenObject = elgg_get_entities(array(
'type' => 'object',
'subtype' => $os.'Tokens',
'limit' => 1 // only grab one
));
$tokens = $tokenObject[0]->tags; // get tag data for first (and only) entity

How to create nodes using node_save?

I'm trying to migrate my current html site into drupal. I have over 80,000 pages I have to migrate so I thought instead of sitting infront of a computer for 50 years I would create a module. I was able to create a script that extracts the html from each directory and now I got to a road block where I need to create a node. I'm trying to create a new node using node_save(), but when node_save is executed, I get a PDOException error with everything I try. I'm passing in $node, which is an array which is then casted into an object.
PDOException: in field_sql_storage_field_storage_write() (line 424 of /srv/www/htdocs/modules/field/modules/field_sql_storage/field_sql_storage.module).
This is how we are currently creating the node, but it produces an error:
$node= array(
'uid' => $user->uid,
'name' => $user->name,
'type' => 'page',
'language' => LANGUAGE_NONE,
'title' => $html['title'],
'status' => 1,
'promote' => 0,
'sticky' => 0,
'created' => (int)REQUEST_TIME,
'revision' => 0,
'comment' => '1',
'menu' => array(
'enabled' => 0,
'mlid' => 0,
'module' => 'menu',
'hidden' => 0,
'has_children' => 0,
'customized' => 0,
'options' => array(),
'expanded' => 0,
'parent_depth_limit' => 8,
'link_title' => '',
'description' => '',
'parent' => 'main-menu:0',
'weight' => '0',
'plid' => '0',
'menu_name' => 'main-menu',
),
'path' => array(
'alias' => '',
'pid' => null,
'source' => null,
'language' => LANGUAGE_NONE,
'pathauto' => 1,
),
'nid' => null,
'vid' => null,
'changed' => '',
'additional_settings__active_tab' => 'edit-menu',
'log' => '',
'date' => '',
'submit' => 'Save',
'preview' => 'Preview',
'private' => 0,
'op' => 'Save',
'body' => array(LANGUAGE_NONE => array(
array(
'value' => $html['html'],
'summary' => $link,
'format' => 'full_html',
),
)),
'validated' => true,
);
node_save((object)$node);
// Small hack to link revisions to our test user.
db_update('node_revision')
->fields(array('uid' => $node->uid))
->condition('vid', $node->vid)
->execute();
Usually I create a bulkimport.php script in the document root to do this kind of thing.
Here is the code I use for Drupal 7:
<?php
define('DRUPAL_ROOT', getcwd());
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
function _create_node($title="", $body="", $language="und") {
$node = (object)array();
$node->uid = '1';
$node->name = 'admin';
$node->type = 'page';
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->revision = 1;
$node->language = $language;
$node->title = $title;
$node->body[$language][0] = array(
'value' => $body,
'format' => 'full_html',
);
$node->teaser = '';
$node->log = 'Auto Imported Node';
node_submit($node);
node_save($node);
return $node;
} ### function _create_node
$sith = array(
'Darth Vader' => 'Master: Darth Sidious',
'Darth Sidious' => 'Master: Darth Plagous',
'Darth Maul' => 'Master: Darth Sidious',
'Darth Tyranous' => 'Master: Darth Sidious',
);
foreach($sith as $title=>$body) {
print "Creating Node. Title:[".$title."] \tBody:[".$body."] ";
$node = _create_node($title, $body);
print "\t... Created Node ID: [".$node->nid."]\n";
#print_r($node);
} ### foreach
I was getting exactly the same error as in your original post -
PDOException: in field_sql_storage_field_storage_write()
- etc.
I was already using the stdClass code style shown in the comments above.
The problem turned out to be the pound signs and accented chars in the string I was assigning to the Body field; the strings were coming from a Windows text file.
Converting the string to the Drupal target encoding (UTF-8) worked for me:
$cleaned_string = mb_convert_encoding($html['html'], "UTF-8", "Windows-1252");
$node->body[LANGUAGE_NONE][0]['value'] = $cleaned_string;
$node->body[LANGUAGE_NONE][0]['format'] = 'plain_text';
node_save($node);
Hope this helps someone.
are you using some CMS engine, or it is custom-written website?
in first case, look here http://drupal.org/documentation/migrate
I strongly recommend you to look at the given modules, they should help. Also, as an option, to migrate DB.
You don't need a lot of those blank fields. Also, you can cast the node as an object rather than an array converted to an object.
Your code should be able to be accomplished with this much shorter snippet:
$node = new stdClass();
$node->title = $html['title'];
$node->type = 'page';
$node->body['und'][0]['value'] = $html['html'];
node_save($node);
Also, there's a number of methods that have been developed to mass-import nodes into Drupal - I am a fan of the Feeds module (http://drupal.org/project/feeds). This may require writing a method of exporting your existing content to an intermediary format (CSV or XML), but it works reliably and you can re-import nodes to update content when required.

Categories