PHP Api Object Query - php

I'm having a bit of an issue with an API document I'm currently working with. The code I've been testing just doesn't seem to work, and I've been looking at it for that long now, I just cannot see where I'm going wrong.
API Documentation Screenshot here: https://ibb.co/CW4HRR9
// POST /package/{id}/web/ftpusers
$args = [
'update' => [
'ftp' => [
'id' => '12345',
'user' => ['Password' => 'Password123']
],
],
];
$response = $services_api->postWithFields("/package/".$test_domain_id."/web/ftpusers", $args);
echo '<pre>';
print_r($response);
echo '</pre>';
It just doesn't seem to work, and I'm guessing I'm doing something wrong where it states object and object[]

Looks like I was missing an array object as I've now got this working (extra array within 'ftp'
$args = [
'update'=> [
'ftp' => [
[
'id' => $info[0]->Id,
'user' => [
'Enabled' => true,
'Password' => 'Password123'
],
]
],
],
];

Related

using php guzzle for PUT with stream and parameters

I'm trying to figure out how to implement the following REST PUT using Guzzle. I've tried using multipart and json, but I'm not sure what the syntax should be for having the parameters with a streamed xml file.
Can anyone help me translate this into a proper query? Below is what I tried so far, but it returns an error because it says the ProjectXML must be included.
$newUri = sprintf('%s/projects/%s/storage', $api_base, $testPid);
$newResp = $client->put($newUri,
[
'headers' => [
'Authorization' => sprintf("HubApi %s", $api_key),
],
'multipart' => [
[
'name' => 'ProjectXml',
'contents' => file_get_contents("xml/$testPid.xml")
],
[
'name' => 'ProjectId',
'contents' => $testPid
],
[
'name' => 'HubUserId',
'contents' => "xxxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxx"
],
[
'name' => 'ProductId',
'contents' => "$(package:ourcompany/ourproducttype)/products/ProductABC123"
],
[
'name' => 'ThemeUrl',
'contents' => "$(package:ourcompany/ourproducttype)/themes/themename-white-Classic"
],
]
]
);
Response:
`400 Bad Request - Validation Error` response:
{"code":"RequestValidationError","message":"'Project Xml' should not be empty."}

Getting data as array instead of stdClass in zf-rest

I'm using zf-rest for building my RESTful web apps. I have the following configurations:
'zf-rest' => [
Resource\FeedbackResource::class => [
'listener' => Resource\FeedbackResource::class,
'route_name' => 'api/rest/feedback',
'entity_http_methods' => [
],
'collection_http_methods' => [
'POST',
],
],
],
'zf-content-validation' => [
Resource\FeedbackResource::class => [
'use_raw_data' => false,
'allows_only_fields_in_filter' => true,
'POST' => Resource\FeedbackResource::class . '\\Validator',
],
],
'input_filter_specs' => [
Resource\FeedbackResource::class . '\\Validator' => [
Resource\FeedbackResource::PARAM_NAME => $inputFilterSpecForStrings,
Resource\FeedbackResource::PARAM_EMAIL => $inputFilterSpecForStrings,
],
],
Then I created the resource with the corresponding method:
class FeedbackResource extends AbstractResourceListener
{
public function create($data)
{
// do something
}
}
I posted a json string to the endpoint and everything works fine so far. But what I'm wondering about is that I will get $data as an object with the json data as attributes. I expected to get an assoziative array. Is this possible?

Create Associative Array with Foreach, Insert into existing Associative Array

Hello.
I currently have a problem with the AWS Route-53 API. To create a record you need to call a function, which itself needs an array of inputs.
I want to create a record set here and for that I have some POST values. One of them, $_POST['record_value'], is a textarea and has multiple lines. I loop through them. This is to enable multiple values for one record. The code is as follows when you hardcode it as one value in ResourceRecords;
$result = $this->route53->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'CREATE',
'ResourceRecordSet' => [
'Name' => $recordName,
'ResourceRecords' => [
[
'Value' => $recordValue
],
],
'TTL' => $recordTtl,
'Type' => $recordType,
],
],
],
'Comment' => 'Routing Record Set',
],
'HostedZoneId' => $this->zone,
]);
Hower. I want to make ResourceRecords dynamically. For every line in the textarea I need a new set of the following part of the code;
[
'Value' => $recordValue
],
What I thought is the following;
$newData = [];
foreach(explode("\r\n", $recordValue) as $valLine) {
$newData[] = ["Value" => $valLine];
}
$result = $this->route53->changeResourceRecordSets([
'ChangeBatch' => [
'Changes' => [
[
'Action' => 'CREATE',
'ResourceRecordSet' => [
'Name' => $recordName,
'ResourceRecords' => [
$newData
],
'TTL' => $recordTtl,
'Type' => $recordType,
],
],
],
'Comment' => 'Routing Record Set',
],
'HostedZoneId' => $this->zone,
]);
However, this seems to return an exception: Found 1 error while validating the input provided for the ChangeResourceRecordSets operation:↵[ChangeBatch][Changes][0][ResourceRecordSet][ResourceRecords][0] must be an associative array. Found array(1).
Am I building the array wrong or am I doing this wrong alltogether?
$newData is already an array, you don't need to wrap it in another array.
'ResourceRecords' => $newData,

PHP Guzzle POST Request Returning Null

I'm racking my brains over this - the request goes fine in Postman, but I can't access the body using Guzzle.
$client = new \GuzzleHttp\Client();
$res = $client->request('POST', 'https://apitest.authorize.net/xml/v1/request.api', [
'headers' => [
'Content-Type' => 'application/json',
],
'body' => json_encode([
'createTransactionRequest' => [
'merchantAuthentication' => [
'name' => '88VuW****',
'transactionKey' => '2xyW8q65VZ*****'
],
'transactionRequest' => [
'transactionType' => 'authCaptureTransaction',
'amount' => "5.00",
'payment' => [
'opaqueData' => [
'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
'dataValue' => $_POST['dataValue']
]
]
]
]
])
]);
dd(json_decode($res->getBody()));
The above returns null - no errors, just null. The below is the same request in Postman, successful with a body.
Any ideas would be really appreciated, thanks!
You should use $res->getBody()->getContents().
And json_decode() returns null in case of any error. You have to check them separately (unfortunately) with json_last_error().

Can't update map column of DynamoDB table

I am currently developing a skill for Amazon's echo dot which requires the use of persistent data. I ran into an issue when developing a web interface for my skill where I was not able to easily update the mapAttr column of the DynamoDB table used by the skill.
I've been trying to work this out for the last 2 days, I've looked everywhere including the documentation but can't seem to find anything that'll help me.
This is the code I am using:
$result = $client->updateItem([
'TableName' => 'rememberThisDBNemo',
'Key' => [
'userId' => [ 'S' => $_SESSION['userDataAsk'] ]
],
'ExpressionAttributeNames' => [
'#attr' => 'mapAttr.ReminderJSON'
],
'ExpressionAttributeValues' => [
':val1' => json_encode($value)
],
'UpdateExpression' => 'SET #attr = :val1'
]);
I have tried many different things so this might be just absolutely wrong, but nothing that I have found has worked.
The table has 2 columns, userId and mapAttr, userId is a string and mapAttr is a map. Originally I thought it was simply a JSON string but it was not like that as when I tried to update it with a JSON string directly it would stop working when read by Alexa.
I am only trying to update 1 out of the 2 attributes of mapAttr. That is ReminderJSON which is a string.
Any help would be appreciated. Thanks.
Try calling updateItem like this
$result = $client->updateItem([
'TableName' => 'rememberThisDBNemo',
'Key' => [
'userId' => [ 'S' => $_SESSION['userDataAsk'] ]
],
'ExpressionAttributeNames' => [
'#mapAttr' => 'mapAttr',
'#attr' => 'ReminderJSON'
],
'ExpressionAttributeValues' => [
':val1' => ['S' => json_encode($value)]
],
'UpdateExpression' => 'SET #mapAttr.#attr = :val1'
]);
However, please be aware that in order for this to work, attribute mapAttr must already exist. If it doesn't, you'll get ValidationException saying The document path provided in the update expression is invalid for update...
As a workaround, you may want to add a ConditionExpression => 'attribute_exists(mapAttr)' to your params, catch possible exception, and then perform another update adding a new attribute mapAttr:
try {
$result = $client->updateItem([
'TableName' => 'rememberThisDBNemo',
'Key' => [
'userId' => [ 'S' => $_SESSION['userDataAsk'] ]
],
'ExpressionAttributeNames' => [
'#mapAttr' => 'mapAttr'
'#attr' => 'ReminderJSON'
],
'ExpressionAttributeValues' => [
':val1' => ['S' => json_encode($value)]
],
'UpdateExpression' => 'SET #mapAttr.#attr = :val1'
'ConditionExpression' => 'attribute_exists(#mapAttr)'
]);
} catch (\Aws\Exception\AwsException $e) {
if ($e->getAwsErrorCode() == "ConditionalCheckFailedException") {
$result = $client->updateItem([
'TableName' => 'rememberThisDBNemo',
'Key' => [
'userId' => [ 'S' => $_SESSION['userDataAsk'] ]
],
'ExpressionAttributeNames' => [
'#mapAttr' => 'mapAttr'
],
'ExpressionAttributeValues' => [
':mapValue' => ['M' => ['ReminderJSON' => ['S' => json_encode($value)]]]
],
'UpdateExpression' => 'SET #mapAttr = :mapValue'
'ConditionExpression' => 'attribute_not_exists(#mapAttr)'
]);
}
}

Categories