The primary goal that I'm trying to achieve is to iterate over my running EC2 instances in PHP.
It's really easy to get the data using a bash script, as shown below:
Bash script:
#!/bin/bash
export AWS_ACCESS_KEY_ID="AKIDEXAMPLE"
export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY"
aws ec2 describe-instances --region="eu-west-1" --filter "Name=instance-state-name,Values=running"
Bash output:
{
"Reservations": [
{
"OwnerId": "58728357357",
"ReservationId": "r-0e0283649826935",
"Instances": [
{
"SecurityGroups": [
{
"GroupId": "sg-2fe333148",
"GroupName": "WEB"
}
],
"PublicDnsName": "ec2-53-13-121-72.eu-west-1.compute.amazonaws.com",
"Architecture": "x86_64",
"LaunchTime": "2016-07-11T08:28:23.000Z",
"RootDeviceName": "/dev/sda1",
"BlockDeviceMappings": [
{
"Ebs": {
// ...
}
]
}
However, when I try the following example, using the same keys, I am presented with what seems to be an unusable object - or at least the object looks like it is representing an empty data structure.
PHP File:
<?php
require __DIR__ . "/vendor/autoload.php";
$settings = [
"version" => "latest",
"region" => "eu-west-1",
"credentials" => [
"key" => "AKIDEXAMPLE",
"secret" => "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
],
];
$client = new \Aws\Ec2\Ec2Client($settings);
$result = $client->describeInstances([
"Filters" => [
[
"Name" => "instance-state-name",
"Value" => "running",
]
],
]);
var_dump($result);
PHP Output:
What the hell am I meant to do with this AWS\Result?
class Aws\Result#82 (1) {
private $data =>
array(2) {
'Reservations' =>
array(0) {
}
'#metadata' =>
array(4) {
'statusCode' =>
int(200)
'effectiveUri' =>
string(35) "https://ec2.eu-west-1.amazonaws.com"
'headers' =>
array(5) {
...
}
'transferStats' =>
array(1) {
...
}
}
}
}
Am I missing something in the PHP configuration? Please can someone help point me in the right direction?
P.S. I've masked the API keys in the above examples.
EC2::DescribeInstances takes an array of filters, each of which has a string Name and an array of string Values. In your CLI example, you've supplied something for Values, whereas in your PHP example you've supplied a Value instead. This field is not recognized by the SDK and will be ignored. See the SDK API docs for more information.
Your PHP should be updated to read:
<?php
require __DIR__ . "/vendor/autoload.php";
$settings = [
"version" => "latest",
"region" => "eu-west-1",
"credentials" => [
"key" => "AKIDEXAMPLE",
"secret" => "wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY",
],
];
$client = new \Aws\Ec2\Ec2Client($settings);
$result = $client->describeInstances([
"Filters" => [
[
"Name" => "instance-state-name",
"Values" => ["running"],
]
],
]);
var_dump($result);
Related
I'm using the Merchant Fulfillment portion of the Amazon Selling Partner API found at https://github.com/jlevers/selling-partner-api/
I have other portions working... namely orders coming down and reports going up. But I am using the same techniques and the Merchant Fulfillment API is not working. The response I get back indicates that it's not received the data I am sending it.
What am I doing wrong?
// North America Live
$NA = [
'url' => 'https://sellingpartnerapi-na.amazon.com',
'region' => 'us-east-1',
];
// North America Sandbox
// $NA = [
// 'url' => 'https://sandbox.sellingpartnerapi-na.amazon.com',
// 'region' => 'us-east-1',
// ];
$config = new SellingPartnerApi\Configuration([
"lwaClientId" => $lwaClientId,
"lwaClientSecret" => $lwaClientSecret,
"lwaRefreshToken" => $lwaRefreshToken,
"awsAccessKeyId" => $awsAccessKeyId,
"awsSecretAccessKey" => $awsSecretAccessKey,
"endpoint" => $NA // or another endpoint from lib/Endpoint.php
]);
$apiInstance = new SellingPartnerApi\Api\MerchantFulfillmentApi($config);
$body = new \SellingPartnerApi\Model\MerchantFulfillment\GetEligibleShipmentServicesRequest(
[
"shipment_request_details" =>
[
"amazon_order_id" => "XXX-XXXXXXXX-XXXXXXXX",
"item_list" =>
[
"order_item_id" => "XXXXXXXXXXXXXXX",
"quantity" => 1,
],
"ship_from_address" =>
[
"name" => "XXXXXXX",
"address_line1" => "XXXXXXXXXX",
"email" => "XXXXX#XXXXX.COM",
"city" => "XXXXXXXXX",
"postal_code" => "XXXXXX",
"country_code" => "US",
"phone" => "XXXXXXXXXXXX",
],
"weight" =>
[
"value" => 7,
"unit" => "oz",
],
"shipping_service_options" =>
[
"delivery_experience" => "DeliveryConfirmationWithSignature",
"carrier_will_pick_up" => true,
]
]
]
);
try {
$result = $apiInstance->getEligibleShipmentServices($body);
d($result);
} catch (Exception $e) {
echo "<PRE>";
echo 'Exception when calling MerchantFulfillmentV0Api->getEligibleShipmentServices: <BR>', wordwrap($e->getMessage(),80,"<br>\n",TRUE), PHP_EOL;
echo "</PRE>";
}
The $response I get back is:
Exception when calling MerchantFulfillmentV0Api->getEligibleShipmentServices:
[400] {
"errors": [
{
"code": "InvalidInput",
"message": "5
validation errors detected: Value \u0027\u0027 at
\u0027shipmentRequestDetails.shipFromAddress.email\u0027 failed to satisfy
constraint: Member must satisfy regular expression pattern: .+#.+; Value
\u0027\u0027 at \u0027shipmentRequestDetails.amazonOrderId\u0027 failed to
satisfy constraint: Member must satisfy regular expression pattern:
[0-9A-Z]{3}-[0-9]{7}-[0-9]{7}; Value \u0027\u0027 at
\u0027shipmentRequestDetails.weight.unit\u0027 failed to satisfy constraint:
Member must satisfy enum value set: [g, ounces, oz, grams]; Value null at
\u0027shipmentRequestDetails.weight.value\u0027 failed to satisfy constraint:
Member must not be null; Value \u0027[]\u0027 at
\u0027shipmentRequestDetails.itemList\u0027 failed to satisfy constraint: Member
must have length greater than or equal to 1",
"details": ""
}
]
}
In the Shipping Service options.... this needs to be set false.
"carrier_will_pick_up" => true,
If its set to true then it will not work. I think it has something to do with sending a request for pickup to the carrier.... and it seems counter-intuitive to set it to false... but thats what made it work!
I'm having a hard time using a method setData() of the HttpBody class. I'm passing the parameter to the method as an object, but I recieve an error message.
How do I pass the parameter:
public function create(string $resource, $body)
{
$client = $this->googleClient();
$service = new CloudHealthcare($client);
$parent = "projects/my_project_id/locations/my_location/datasets/my_dataset/fhirStores/repository";
$httpBody = new HttpBody();
$httpBody->setContentType('application/fhir+json;charset=utf-8');
$httpBody->setData([
"resourceType" => "Patient",
"id" => "23434",
"meta" => [
"versionId" => "12",
"lastUpdated" => "2014-08-18T15:43:30Z"
],
"text" => [
"status" => "generated",
"div" => "<!-- Snipped for Brevity -->"
],
"extension" => [
[
"url" => "http://example.org/consent#trials",
"valueCode" => "renal"
]
],
"identifier" => [
[
"use" => "usual",
"label" => "MRN",
"system" => "http://www.goodhealth.org/identifiers/mrn",
"value" => "123456"
]
],
"name" => [
[
"family" => [
"Levin"
],
"given" => [
"Henry"
],
"suffix" => [
"The 7th"
]
]
],
"gender" => [
"text" => "Male"
],
"birthDate" => "1932-09-24",
"active" => true
]);
$data = $service->projects_locations_datasets_fhirStores_fhir->create($parent, $resource, $httpBody);
return $data;
}
Following the error message I get.
The error says I didn't pass the resourceType field, but it was passed:
Google\Service\Exception: {
"issue": [
{
"code": "structure",
"details": {
"text": "unparseable_resource"
},
"diagnostics": "missing required field \"resourceType\"",
"expression": [
""
],
"severity": "error"
}
],
"resourceType": "OperationOutcome"
} in file /usr/share/nginx/vendor/google/apiclient/src/Http/REST.php on line 128
How should I pass the parameter to receive the success message?
Tkanks!
I haven't tried the PHP client libraries specifically, but for most languages you don't want to use the HttpBody class - it's an indication that the method accepts something in the body of the request that is just text from the perspective of the method signature. I would try passing the JSON string directly.
I am trying to write I functional test for my deep cloning entity method __clone() on my Symfony project, which is defined in my method class..
I am new at writing tests and I can not find any source where I can get an info about this specific operation. This is what I got so far:
public function testDuplicate()
{
$userInProgress = $this->fixtureLoader->getReferenceRepository()->getReference(UserFixtures::USER_1);
$data = [
"name" => "John Doe"
];
$expected = [
"name" => "John Doe",
"status" => "IN_PROGRESS",
"assignee" => [
"uuid" => "4e7c77d5-8e36-4319-b250-050d241621dc",
]
"archived" => FALSE
];
$client->request('POST',
self::API_URL . self::USER_ENDPOINT . 'copy/' . $userInProgress->getId(),
[],
[],
[
'CONTENT_TYPE' => 'application/ld+json',
'HTTP_ACCEPT' => 'application/ld+json'
],
json_encode($data)
);
//assert
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$response = $client->getResponse()->getContent();
$this->assertArraySubset($expected, json_decode($response, true));
}
This will run my test but I am not sure if it will actually test if deep cloning is set as it should?
Can anybody help?
I am trying to highlight my results in elastic search-php , I tried a lot with my knowledge and searching in google, but no luck , the same query is working perfectly in Sense. my query in Sense is
GET /bank/account/_search
{
"query" : {
"match_phrase" : {
"address" : "mill"
}
},
"highlight": {
"pre_tags" : ["<tag1>"],
"post_tags" : ["</tag1>"],
"fields" : {
"address" : {}
}
}
}
with above query i get exact result what i needed, this is the result i got
highlight": {
"address": [
"990 <tag1>Mill</tag1> Road"
]
}
i tried the same query using php i am not get the highlighted results my php query is
<?php
require 'vendor/autoload.php';
$client=new Elasticsearch\Client();
$indexParams = [
'index' => 'bank',
'type' => 'account',
'body' => [
'query' => [
'match' => [
"address" => "mill"
],
],
'highlight' => [
"pre_tags" => "<tag1>",
"post_tags" => "</tag1>",
'fields' => [
'address' => new \stdClass()
]
],
]
];
$results = $client->search($indexParams);
try {
$response = $client->search($indexParams);
} catch (Exception $e) {
var_dump($e->getMessage());
}
echo '<pre>',print_r($response),'</pre>';
?>
the result i aam getting is
[highlight] => Array
(
[address] => Array
(
[0] => 715 Mill Avenue
)
)
i got the answer for above question, i am sending parameters in the form of json and JSON encode the result, when i encode the result in JSON at that pre tags are came in highlight query.
my solution is
"highlight": {
"address": [
"817 Campus </span>Road</span>"
]
}
I was not able to find a working example on how to query Elasticsearch using the completion suggester in PHP (elasticsearch-php).
Querying via CURL, e.g.
curl -X POST 'localhost:9200/tstidx/_suggest?pretty' -d '{
"try" : {
"text" : "a",
"completion" : {
"field" : "suggest"
}
}
}'
works, so the only problem is the query part in PHP.
How do I use the API to query Elasticsearch using the completion suggester?
The PHP ES client has a method called suggest that you can use for that purpose:
$params = [
'index' => 'tstidx',
'body' => [
'try' => [
'text' => 'a',
'completion' => [ 'field' => 'suggest' ]
]
]
];
$client = ClientBuilder::create()->build();
$response = $client->suggest($params);
Using the PHP elasticsearch API
<?Php
include 'autoload.php';
$elastic_search = new ElasticApiService();
$search_params = array(
'index' => 'my_index',
'search_type' => 'match',
'search_key' => 'citynamefield',
'search_value' => 'orlando'
);
$search_results = $elastic_search->searchElastic($search_params);
echo '<pre>';
print_r($search_results);