I'm using PHP to create a system that will fetch webhook payload when someone is unsubscribing newsletters, but I can figure out how to fetch the actual payload information in PHP.
Is there any POST data to fetch? How does PHP look for this POST data?
UPDATE: I may be on to something. Seems like the function http_get_request_body() will do the trick?
$http_get_request_body solves it :)
I recently ran into this issue and used the following PHP code for handling Campaign Monitor Web Hooks:
<?php
$json = file_get_contents('php://input');
$data = json_decode( $json, TRUE ); //convert JSON into array
foreach ($data['Events'] as $event)
{
// Process each entry in the request
}
The JSON data, once converted to an array will give you data in this format:
array (
'ListID' => 'LIST_ID_KEY',
'Events' => array (
0 =>
array (
'Type' => 'Subscribe',
'Date' => '2014-01-01 16:00:00',
'EmailAddress' => 'test#example.com',
'Name' => 'John Smith',
'CustomFields' => array (),
'SignupIPAddress' => 'API',
),
),
)
Related
I am trying to POST JSON encoded data to a webhook endpoint that resides in one of our companies Microsoft Teams channels. It accepts basic JSON encoded payloads.
I started off in PostMan on my local machine and POST'd the following to my Teams Channel connector webhook URL:
{
"#context": "https://schema.org/extensions"
"#type": "MessageCard",
"themeColor": "0072C6",
"title": "Test From PostMan",
"text": "This is the text body of the notification card",
"potentialAction": [
{
"#type": "OpenUri",
"name": "View in Browser...",
"targets": [
{ "os": "default", "uri": "https://<REDACTED>" }
]
}
]
}
This works fine, it posts the card into the Teams Channel, with the action button below it.
So I moved to PHP, and I made the code below:
<?php
//api endpoint
$url = 'https://<REDACTED>';
//new curl connection
$ch = curl_init($url);
//build json data array
$postdata = array(
'#context' => 'https://schema.org/extensions',
'#type' => 'MessageCard',
'themeColor' => '0072C6',
'title' => 'Test from curl in PHP',
'text' => 'test string.. test string.. test string.. test string.. test string.. test string.. test string..',
'potentialAction' => array (
'#type' => 'OpenUri',
'name' => 'View in Browser...',
'targets' => array (
'os' => 'default',
'uri' => 'https://<REDACTED>'
)
)
);
//encode json data array
$encodeddata = json_encode($postdata);
//set curl options
curl_setopt($ch, CURLOPT_POSTFIELDS, $encodeddata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//debug
echo $result;
//close
curl_close($ch);
?>
When I run the above, the API errors and responds saying it was an invalid payload. So I stripped my code down so that my $postdata array was a lot simpler, as below:
//build json data array
$postdata = array(
'#context' => 'https://schema.org/extensions',
'#type' => 'MessageCard',
'themeColor' => '0072C6',
'title' => 'Test from curl in PHP',
'text' => 'test string.. test string.. test string.. test string.. test string.. test string.. test string..'
);
And this works fine, my PHP script is able to post a card into the Teams Channel, just with no action button underneath it. So my issue here lies in how I'm encoding the additional arrays inside of $postdata ?
I'll be honest, my knowledge of arrays in PHP is limited, I think I'm doing this correctly, but clearly I'm having problems, lol. Is there a different/better/more correct way to encode multiple arrays inside of an array into JSON data to POST?
potentialAction in your original JSON is an array of objects, but you made it a single level array only in your PHP data structure.
You need to wrap this into an additional level:
$postdata = array(
'#context' => 'https://schema.org/extensions',
'#type' => 'MessageCard',
'themeColor' => '0072C6',
'title' => 'Test from curl in PHP',
'text' => 'test string.. test string.. test string.. test string.. test string.. test string.. test string..',
'potentialAction' => array (
array (
'#type' => 'OpenUri',
'name' => 'View in Browser...',
'targets' => array (
array (
'os' => 'default',
'uri' => 'https://<REDACTED>'
)
)
)
)
);
This will give you an array of objects in that place, when you encode it as JSON. (The outer array has a zero-based numeric index, so it stays an array when converted to JSON; the inner array has associative keys, so it automatically becomes an object.)
Edit: As mentioned in comments, same thing for the targets property inside. Edited the code, to insert an additional level there as well.
Im working with an api which stores data into a JSON file. This data is gathered from a form that the users fill in my website. The way its inserted goes as follow:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
And then:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
Now instead of post i want to get the data. I tried getting data like this:
$SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=*my name*');
I dont know if this is the right way, well it didn't work so obviously its not.
Anyways what im trying to achieve is with PHP i want to gather the name value of an existing person. this data is stored in /api/v2/crm/person.json
Api documentation (which i read but didn't understand to well) http://api.simplicate.nl/
It's been a while but i'm trying to answer all my open questions without an answer which i ended up solving on my own.
So for this.
You have to create a variable which makes the get request like this:
$test = $SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=My name');
Now you can for example do a var_dump($test);
And as output you will get all the data inside
/crm/organization?q[name]=My name
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.
I know this type of question has been asked a number of times. I have spent several hours reading and trying the offered solutions - but none appear to work for my situation.
I need to send a SOAP request to an API that can contain an element that repeats like so:
<operationNumbers>
<operationNumber>1234</operationNumber>
<operationNumber>1235</operationNumber>
<operationNumber>1236</operationNumber>
<operationNumber>1237</operationNumber>
</operationNumbers>
I did read that perhaps I could do this:
$buildRequest = Array(
'myheader' => Array(
'date' => MY_DATE,
'id' => Array(
'client' => CLIENT,
'clientRef' => MYREF
)
),
'operationNumbers' => Array (
Array('operationNumber' => '1234'),
Array('operationNumber' => '1235')
)
);
$request = $client->__soapCall( 'getMultiOpDets', array($buildRequest) );
Sadly this does not work and results in 'invalid request', if I send in a single operation number eg:
...
'operationNumbers' => Array (
'operationNumber' => '1234'
)
...
The request is successful. I've tried soapVars/soapParams but cannot get it working using this approach. Any hints/tips/help appreciated.
So, I solved it.
$operationNumbersArray = array('1234','1235');
...
'operationNumbers' => array(
'operationNumber' => $operationNumbersArray
)
During my testing and fiddling about, I had inadvertently removed another value that was mandatory. The API did not give warning of it's omission (sadly).
Here is the code I use:
$wsdl = 'https://your.api/path?wsdl';
$client = new SoapClient($wsdl);
$multipleSearchValues = [1, 2, 3, 4];
$queryData = ['yourFieldName' => $multipleSearchValues];
$results = $client->YourApiMethod($queryData);
print_r($results);
I am trying to convert post data into a format that would allow me to pass it right into my collection. For example: When I print_r on the $_POST I get this form data:
Array
(
[Name] => Steve
[Email] => Steve#mail.com
[submit] => Submit
)
I am wondering how i can convert this to an acceptable object to insert into mongodb collection using php similar to:
$Record = array(
'Name' => 'Steve',
'Email' => 'Steve#mail.com',
'submit' => 'Submit'
);
$Collection->insert($Record);
I am thinking a loop of the above array with some additional formatting but I can't seem to figure it out. I have also tried json_encode but keep getting the same error "Call to a member function insert() on a non-object in..." saying that its not a proper object. Thank you for any help.
No need to encode anything, it's just PHP native and expects an array. Let the driver do the work for you:
$Collection->insert( $_POST );
As it is the two should be equivalant:
$rec = array(
'Name' => 'Steve',
'Email' => 'Steve#mail.com',
'submit' => 'Submit'
);
print_r ($rec);