I'm trying to manipulate some fields in a supplied Word 2003 document using the document's own bookmarks using PHP and COM but am getting an error which ever method that I use. If I try to call the Bookmarks directly to substitute the text I get an error: The range cannot be deleted.
function testBkMrkDetails($word, $bookmarkName, $subsTxt) {
try {
$BkMark = $word->ActiveDocument->Bookmarks($bookmarkName);
$range = $BkMark->Range;
if (!$range) {
echo "Range not created ";
}
$range->Text = $subsTxt;
} catch (Exception $e) {
echo "bookmark failed: " . $e->getMessage() . "\n";
}
}
I've searched on Google using the error message and loking for PHP and COM tutorials which appeared to suggest that I look at FormFields so I wrote the following:
function testFormFlds ($word, $bookmarkName, $subsTxt) {
try {
$formField = $word->Selection->FormFields($bookmarkName);
if (!$formField) {
die("Form failed : " . $bookmarkName . " not found\n");
}
$formField->Result($subsTxt);
} catch (Exception $e){
echo "FormField failed: " . $e->getMessage();
}
}
However this keeps telling me that the requested member of the collection does not exists so I'm assuming that I have not called the feild name correctly (it was taen from the document). I would be grateful for some pointers towards solving this and learning more about the underlying technology. Thanks, Iain
Related
I have this kind of loop. On each iteration it should include a file, Included files can come with errors. Once some of included files gets an error the whole process of getting lost. How to prevent breaking of the process?
I tried this try catch but it errors from included files still cause stopping execution of file.
Thanks
foreach ($li_arrays as $index => $li_array) {
if($index == 0){
try {
require 'update.php';
} catch(Exception $e) {
echo "Exception caught with message: " . $e->getMessage() . "\n";
}
}
elseif ($index == 1){
try {
require 'update1.php';
} catch(Exception $e) {
echo "Exception caught with message: " . $e->getMessage() . "\n";
}
}else{
try {
require 'update2.php';
} catch(Exception $e) {
echo "Exception caught with message: " . $e->getMessage() . "\n";
}
}
Errors in php are not recoverable, so they will always lead to the termination of your script.
I am not even sure that you are even talking about Errors, though if you are, this is the answer to your question.
Another thing to be aware of:
Require will throw an E_COMPILE_ERROR if the required file doesn't exist, which is also something you won't be able to catch.
If you don't want to terminate if the script isn't found, use include instead.
At the end I changed "require" with "include" and used this try-catch approach inside each included file.
$attempts = 0;
do {
try
{
////PHP code ///
} catch (Exception $e) {
echo "\n\n======EXCEPTION======\n\n";
var_dump($e);
$attempts++;
sleep(30);
continue;
}
break;
} while($attempts < 5);
I am using php-on-couch library to retrieve data from couchdb
I want to retrieve basing on where condition. I want data according to the user who is signed in. My code below retrieves a view called accounts but i want to be retrieving without creating views on couchdb. Any help is highly appreciated
try {
$doc = $client->asArray()->getView('accounts', 'accounts');
} catch (Exception $e) {
if ($e->code() == 404) {
echo "View not found\n";
} else {
echo "Error: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
}
exit(1);
}
print_r($doc);
is there a better way to dynamically create a view on retrieving just like mysql does?
using php-on-couch you can do this
try {
$opts = array("include_docs" => true, "key" => "your user name");
$doc = $client->setQueryParameters($opts)->getView("accounts", "accounts");
} catch (Exception $e) {
if ($e->code() == 404) {
echo "Account not found\n";
} else {
echo "Error: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
}
exit(1);
}
print_r($doc);
Make sure you emit the username as the key in the accounts view. Couch db will search in all accounts in the view and give you the doc that contains your username.
emit(doc.username, doc);
Hope it helps the rest of you using couchdb in php
How may I send a draft with GMAIL API and OAuth2.0 via PHP?
In the official docs there is no reference on how to achieve this with PHP.
Based on the Java example, I tried:
$drafts = array();
try {
$draftsResponse = $service->users_drafts->listUsersDrafts('me');
if ($draftsResponse->getDrafts()) {
$drafts = array_merge($drafts, $draftsResponse->getDrafts());
}
}
catch (Exception $e) {
echo 'An error occurred: ' . $e->getMessage();
}
var_dump($drafts);
foreach ($drafts as $draft) {
echo 'Draft with ID: ' . $draft->getId() . '<br/>';
$abc = $service->users_drafts->send('me',$draft->getId());
var_dump($abc);
}
But of course I am doing something wrong, because it is not working. The first var_dump() returns all drafts. But nothing else happens after that.
Can you please help me?
You have to create a new Google_Service_Gmail_Draft instance and use that, not just supply the id:
foreach ($drafts as $draft) {
$d = new Google_Service_Gmail_Draft();
$d->setId($draft->getId());
$service->users_drafts->send('me', $d);
}
I was able to install the Parse PHP SDK (version 1.1.2) to my project and got through the quick start guide in successfully creating a TestObject. However, when I tried to query the objects, it returns nothing. I'm stuck and don't know where to go from here.
use Parse\ParseQuery;
$query = new ParseQuery('TestObject');
$results = $query->find();
foreach($results as $result){
echo $result->getObjectId() ." - " $result->get("foo") . "\n";
}
You could try it this way:
$myObj= new ParseObject("TestObject");
$myObj->set("objIdd", 1234);
$myObj->set("objName", "My Name");
try {
$myObj->save();
echo 'New object created with objectId: ' . $myObj->getObjectId();
} catch (ParseException $ex) {
// Execute any logic that should take place if the save fails.
// error is a ParseException object with an error code and message.
echo 'Failed to create new object, with error message: ' + $ex->getMessage();
}
//objectId: "XYZABC", objIdd: 1234, objName: "My Name"
$query = new ParseQuery("TestObject");
try {
$myObj = $query->get("XYZABC");
// The object was retrieved successfully.
} catch (ParseException $ex) {
// The object was not retrieved successfully.
// error is a ParseException with an error code and message.
}
Take a look at this guide.
I'm creating a module for joomla that lists the the contents of a given folder. This module works as a service, so no OAuth should be required.
I'm stuck since every request I make has the same answer, no items. So I would like to know if I'm creating the client/service in a wrong way or I may have registered the application in a wrong way.
Here is the code:
// this class is where I have all the IDs from the google console
require_once(dirname(__FILE__) . '/gdrive/config.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/apiClient.php');
require_once(dirname(__FILE__) . '/gdrive/gd-v2-php/contrib/apiDriveService.php');
$client = new apiClient();
$key = file_get_contents(Config::KEY_FILE);
$client->setClientId(Config::CLIENT_ID);
$client->setUseObjects(true);
$client->setAssertionCredentials(new apiAssertionCredentials(
Config::SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/drive'), $key)
);
$service = new apiDriveService($client);
try {
$parameters = array();
$parameters['maxResults'] = $maxResults;
$files = $service->files->listFiles($parameters);
$items = $files->getItems();
var_dump($items);
$pageToken = $files->getNextPageToken();
} catch (apiServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
// Error message is formatted as "Error calling <REQUEST METHOD> <REQUEST URL>: (<CODE>) <MESSAGE OR REASON>".
print "Error message: " . $e->getMessage() . "\n";
} catch (apiException $e) {
// Other error.
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
} catch (Exception $e) {
print "Cannot retrieve the files";
print($e->getMessage());
}
Sorry to ask such a noob question but this is driving me crazy
Thanks
P.S. just for the record, I followed this google-api-php-client/wiki/OAuth2#Service_Accounts
After having a second look to the documentation I found that Google Drive does not support service accounts. I don't know if this is going to change but, with the requirements I have (I need to show in the web is what an end user uploads to a folder (which is public) using the desktop app) I think I cannot use google drive as a valid solution.
I found a solution but I don't like to much since I should have to refresh the token forever Google Drive SDK Service Account Error