Can we use post method for getting data? - php

I've many parameters(filters) for getting data from API so i've used POST method for sending parameters(filters) to API . Its working fine but I want to know its ok to use POST function for getting list from API ?
Reason for using POST instead GET :
If I've used GET function it will create problem for array data and also We need to pass some string data that may contain some special characters so in GET method need lot off manual error handling.
Our application under development phase so when new parameter need to add every time we need to change the URL on all our applications where we use this API.
$filter = (object) array(
'page_name' => '',
'page_category' => 0,
'page_gender' => 'N',
'page_country' => 0,
'page_company' => 0,
'page_language' => 0,
'page_order' => 'ASC',
'page_order_field' => 'dlang_firstname',
'page_currency' => 47,
'page_currency_unit' => 1,
'page_currency_iso' => 'GBP',
'page_current' => $page,
'page_size' => env('PAGE_SIZE', 10),
);
Above data I've passing to API.Is there any other way to send data ?

Related

How to add a "pipe" into a get parameter during guzzle query building

I'm using an api that has a "range" parameter that can apply to several different parameter items. The range i'm focusing on is "price". I'm using guzzle in laravel and according to the api documentation, the query for this particular parameter should be written like this "&range_facet=price|500|2500|250"...this is broken down into the minimum, maximum, and interval values of the price range parameter. That's not necessarily important to this question. When i try and run this query as is, i get nothing returned. When I remove that particular parameter, i get values but obviously they're not filtered the way i want them to be. When i run this in Insomnia, the pipes are replaced by "%7C", which is obviously (obviously?) not interpreted by the api as it's not how it's waiting for the GET request to be made. How can I insert the pipes into the query so that it calls the correct way?
I've tried to create an additional nested array with the price value being broken up into key value pairs but that didn't work either.
'range_facets' => ['price'['start'=>'500', end=>'2500', 'interval'=>'250']],
$client = new Client();
$result = $client->request('GET', "http://api.example.com", [
'headers' => [
'Host' => 'example-host',
'Content-Type' => 'application/json'
],
'query' => [
'api_key' => 'my_api_key',
'range_facets' => 'price|500|2500|250',
'year' => $year,
'latitude' => '30.170222',
'longitude' => '92.01320199',
'radius' => 500,
'start' => 0,
'rows' => 50
]
]);
I'd like to filter my prices but I need the pipe to be able to do it.
This is exactly how it should be. %7C should be decoded on the server side to | automatically (about query string encoding).
I bet the issue is in different place.

How to create a new source and set as default source on Stripe API

I'm looking for a way to create a new source for a customer and then set that source as the default source for the customer. The problem I'm currently facing is that the response from creating the source does not give me a easy way to identify the id for the new created source so that I can set that source as default.
Below is my code, without any API key and customer ID:
\Stripe\Stripe::setApiKey($stripe_api_key);
$customer = \Stripe\Customer::retrieve($_SESSION['parent']['stripe_customer_id']);
$customer->sources->create(array('source' => array("object" => "card", 'exp_month' => $expire_month, 'exp_year' => $expire_year, 'number' => $card_number, 'address_line1' => $billing_address1, 'address_line2' => $billing_address2, 'address_city' => $billing_city, 'address_zip' => $billing_postal_code, 'address_country' => $billing_country, 'currency' => 'GBP', 'cvc' => $security_code, 'name' => $_SESSION['parent']['firstname'] . ' ' . $_SESSION['parent']['lastname'])))->__toArray(true);
//set as default
if (isset($_POST['check_default_source'])) {
$customer->default_source=$customer['id'];
$customer->save();
}
I also had this problem, slightly different.
I added a new source from a token_id.
The getLastResponse method was not available in my API or library version.
But using the token object (Stripe::Token::retreive( token_id )) you can actually retrieve the card_id, and use that to update the default_source.
Took me a long time to figure this out, so I post it here. Maybe it helps someone in the future...
I was able to to find the solution to my question. Stripe API does send back the id of the source that was just created and to get the ID I use the below code:
$source_id = json_decode($customer->sources->getLastResponse()->body)->id;

Translate value before sending information

is there a away to translate values in php using either google api translate or any other api...
<?php
// 1.- Query to get information
// 2.- build array with that query
// Example array from query
$data = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa'
),
);
// Now that the array has been build, lets make a translation
// Which I have no idea how to do that but the final array should be
$final = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde',
'name_en' => 'Shoes',
'color_en' => 'Green'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa',
'name_en' => 'House',
'color_en' => 'Pink'
),
);
is this process possible or am I just dreaming?
I have very little knowledge on how exactly Goolge API works since I only use the Google Translate widget and the translation is after you present the information but in this case we need to make a translation before presenting the information...
Google translate API is a paid service. You need to get a api key from google api services :
google translate API
After that, you can make a curl to google api after getting your results from query :
sample url for curl :
https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff
You will get the results as JSON object,do json_decode and add results to your array.

In Google Custom Search API for searchType=image what is the expected value for the "rights" field?

I'm using the PHP sdk to do image searches
$service = new Google_Service_Customsearch($client);
$optParams = array(
'imgType' => 'photo',
'imgColorType' => 'color',
'imgSize' => 'large',
'searchType' => 'image',
'num' => '5',
'safe' => 'medium',
'rights' => '(cc_publicdomain|cc_attribute|cc_sharealike)',
'filter' => '1',
'cx' => <my cx>,
);
$results = $service->cse->listCse($query, $optParams);
but i'm unclear that the "rights" field is doing anything.
It seems to generating a curl request that looks like this...
https://www.googleapis.com/customsearch/v1?
q=<query here>
&imgType=photo
&imgColorType=color
&imgSize=large
&searchType=image
&num=6
&safe=medium
&rights=%28cc_publicdomain%7Ccc_attribute%7Ccc_sharealike%29
&filter=1
&cx=<my cx>
but i'm not clear that is working the way i expect. my query returns different images then when I use the web form.
Additionally the docs unhelpfully states..
rights | string | Filters based on licensing. Supported values include:
| | cc_publicdomain, cc_attribute, cc_sharealike, cc_noncommercial,
| | cc_nonderived, and combinations of these.
however i never get an error with this field, i can put any junk i want into it.
what is the proper format google expects here? comma delimited? array? piped ?
Also, the options listed in the docs do not seem to match up precisely with the options in the web form, and some insights there would be nice too.
You can see here for licencing of custom search
https://developers.google.com/custom-search/docs/structured_search#licensing

PHRets Searches returning nothing

I have been trying to set up an automated script to pull from a RETS server using PHP and I can not get the PHRets search to return anything. My query is below:
/* Search RETS server */
$search = $rets->SearchQuery(
'Property', // Resource
3, // Class
'(LastTransDate_f77=2013-01-01+)', // DMQL
array(
'Format' => 'COMPACT-DECODED',
'Select' => 'AREA_f16',
'Count' => 1,
'Limit' => 20,
)
);
EDIT: I got this working finally. The problem had nothing to do with my code, however, it was in the MLS board's faulty documentation. The documentation said that I could select certain fields for certain property types, but that wasn't the case. I had to consult a third party developer and get the list of fields/column names that he had used.
Consult the PHRETS documentation. You have the second parameter as an integer. It should be a string. Also the comma needs to be removed after the last element in the array.
/* Search RETS server */
$search = $rets->SearchQuery(
'Property',
'3',
'(LastTransDate_f77=2013-01-01+)',
array(
'Format' => 'COMPACT-DECODED',
'Select' => 'AREA_f16',
'Count' => 1,
'Limit' => 20
));

Categories