I'm using cURL to get all email from user via Google API. Following https://developers.google.com/admin-sdk/email-audit/#retrieving_all_email_monitors_of_a_source_user.
According this tutorial, the server return '201 Created' status code to successful. But, my result return '200 OK' code.
Here is code Authorization
$data = array(
'accountType' => 'HOSTED_OR_GOOGLE',
'Email' => 'myEmail',
'Passwd' => 'myPassword',
'source'=>'PHP-cUrl-Example',
'service'=>'apps');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
And here is code to Retrieving all email monitors of a source user
preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
$auth = $matches[1];
$header = array('Content-Type: application/atom+xml; charset=utf-8',
'Authorization: GoogleLogin auth='.trim($auth),
);
$url_email ="https://apps-apis.google.com/a/feeds/compliance/audit/mail/monitor/mydomain/username";
curl_setopt($ch, CURLOPT_URL, $url_email);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
$response = simplexml_load_string($response);
curl_close($ch);
print_r($response);
Help me pls ?
The API allows you to request the status of a single export request with a URL of:
https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/{domain name}/{source user name}/{mailbox requestId}
or of all requests across the domain with a request of:
https://apps-apis.google.com/a/feeds/compliance/audit/mail/export/{domain name}?fromDate={fromDate}
there is no operation to retrieve the status of all requests for a given user like you are trying to do.
I suggest you confirm you've successfully created an audit request by using GAM to create the request. GAM will show you the request ID on success. Then you can try getting the results of the single request with your code.
Related
I will admit, I have been on this for 3 days, back and forth through documentation, and even learned how to create my own products and plans through Postman.
So I have a complete working subscription button, now I learned I need to save the Subscription ID in order to cancel the subscription so now that is saving and loading perfectly.
To my sad surprise, I need to get an AuthKey every time someone wants to cancel, so I need to run 2 cURL commands, 1 GET Auth Key, and 1 POST cancel a subscription. How would I do this in PHP?
I don't get any errors when these commands run just no data.
Paypal cURL Example:
https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
////////////////////////////// GET TEMP ACCESS TOKEN/////////////////////////////////
$ch = curl_init();
$clientId = "x";
$secret = "x";
$myIDKEY = "";
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r($json->access_token);
$myIDKEY = $json->access_token;
}
curl_close($ch);
/////////////////////////////// SEND CANCEL POST ////////////////////////////////
$ch = curl_init();
$headers = [
'Authorization: Bearer '.$myIDKEY,
'Content-Type: application/json'
];
$postData = [
'reason' => 'clicked cancel subscription button'
];
curl_setopt($ch, CURLOPT_URL,"https://api.sandbox.paypal.com/v1/billing/subscriptions/".$_SESSION['honeybeesubID']."/cancel");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $myIDKEY
Thank you so much!
As documented at https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel , a success response is an HTTP 204 with no data.
So, it will be normal to receive an empty response, along with that status code.
I'm trying to submit a POST request with JSON data to an api endpoint. The endpoint requires a querystring passing the api credentials, but also requires the JSON data to be POSTed.
When I try to do this with PHP cURL as shown below, the querystring is apparently removed - thus the api is rejecting the request due to missing api key.
I can do this easily with Postman when testing access to the api endpoint.
How can I make the cURL request include both the querystring AND the JSON POST body?
Example code:
// $data is previously defined as an array of parameters and values.
$url = "https://api.endpoint.url?api_key=1234567890";
$ch = curl_init();
$json = json_encode($data);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
You are doing almost right.
Sometimes you need to relax SSL verification.
Otherwise, update php ca bundle:
https://docs.bolt.cm/3.7/howto/curl-ca-certificates
Add the following:
$headers = array(
"Content-type: application/json;charset=UTF-8",
"Accept-Encoding: gzip,deflate",
"Content-length: ".strlen($json),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_ENCODING, "identity, deflate, gzip");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Sometimes you need to change encoding too:
$result = utf8_decode($result);
And check the returning data.
I am trying to send a file with php curl, trying to replicate the following python request:
requests.post( f'{url}/sample', data={"forced": True}, files={'sample': open(filepath, 'rb')}, verify=verify_ssl )
Below is my attempt to do the same request using php curl, I however always get an error 400, 'missing sample':
$post = array(
'files'=>array(
'sample'=> file_get_contents($_FILES['file']['tmp_name']),
),
'data'=>array(
'forced'=>TRUE,
),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url.'/sample');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // Skip SSL Verification
$result = curl_exec ($ch);
curl_close ($ch);
What am I doing wrong here ?
That Python request appears to be multipart/form-data and not a JSON request like the PHP example.
To upload a file with curl, see the CURLFile class.
I think it should look like this:
<?php
$sample = new \CURLFile($_FILES['file']['tmp_name']);
$post = [
'forced' => true,
'sample' => $sample,
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url.'/sample');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close($ch);
I'm trying to query multiple domain name availability using php and curl but I can't seem to get a valid response back.
API:
https://api.godaddy.com/v1/domains/available
Documentation here:
https://developer.godaddy.com/doc/endpoint/domains#/v1/availableBulk
My code is below. Any help would be much appreciated.
$domains = array("name.com", "test.com", "monkey123er.com", "Sally123.co");
$domainsJSON = json_encode($domains);
// see GoDaddy API documentation - https://developer.godaddy.com/doc
// url to check domain availability
$url = "https://api.godaddy.com/v1/domains/available".
// see GoDaddy API documentation - https://developer.godaddy.com/doc
// set your key and secret
$header = array(
'Authorization: sso-key XXX:XXXX'
);
//open connection
$ch = curl_init();
$timeout=60;
//set the url and other options for curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // Values: GET, POST, PUT, DELETE, PATCH, UPDATE
curl_setopt($ch, CURLOPT_POSTFIELDS, $domainsJSON);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//execute post and return response data.
$result = curl_exec($ch);
//close curl connection
curl_close($ch);
// decode the json response
$dn = json_decode($result, true);
echo '<pre>'; print_r($dn); echo '</pre>';
I'am developing a sample push notification app in android using c2dm. Here is my PHP code to send the message from server to device.
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {
$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText //TODO Add more params with just simple data instead
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
}
sendMessageToPhone("my application server auth token ","my device id","UTF-8","hello");
But i'am getting "No info." notification on my emulator. Where i'am going wrong ? Please help me.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=$token", "Content-Length: $len", "Content-Type: application/x-www-form-urlencoded"));
echo curl_exec($ch);
curl_close($ch);
This is the php code that my app uses to send C2DM messages where $data is your data array. Please note that the Content-Length is necessary and is is the length of your data.
EDIT: Something you may also find useful a class for php that makes sending messages a little nicer.