-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use API without composer #164
Comments
If you're PHP version doesn't support namespaces (something before v5.3) then I don't think so. You can still use the SparkPost API without the library. |
Your Readme file says "The recomended way to install SparkPost is with composer". Howere I still think there are should be a way to install it without it. |
Thanks for your replay. Is there a guide how to use SparkPost API without the library from PHP? |
You can clone it locally, grab the needed dependencies and transfer them to the application, but I don't know how it will play with unsupported versions of PHP. |
I think I have something that uses the SparkPost API with curl somewhere, one sec :) |
Thanks, I appreciate. |
@yupitomets I didn't find the thing I was thinking of but I whipped this sample up. Let me know if you have any issues with it. <?php
/**
* Use this method to access the SparkPost API
*/
function sparkpost($method, $uri, $payload = [], $headers = [])
{
$defaultHeaders = [ 'Content-Type: application/json' ];
$curl = curl_init();
$method = strtoupper($method);
$finalHeaders = array_merge($defaultHeaders, $headers);
$url = 'https://api.sparkpost.com:443/api/v1/'.$uri;
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if ($method !== 'GET') {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $finalHeaders);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$payload = [
'options' => [
'sandbox' => true,
],
'content' => [
'from' => [
'name' => 'SparkPost Team',
'email' => '[email protected]',
],
'subject' => 'Sending an email with SparkPost',
'html' => '<html><body><h1>Sending mail with PHP+SparkPost+cURL!</h1></body></html>',
],
'recipients' => [
[ 'address' => '[email protected]', ],
],
];
$headers = [ 'Authorization: <YOUR_API_KEY>' ];
echo "Sending email...\n";
$email_results = sparkpost('POST', 'transmissions', $payload, $headers);
echo "Email results:\n";
echo json_encode(json_decode($email_results, false), JSON_PRETTY_PRINT);
echo "\n\n";
echo "Listing sending domains...\n";
$sending_domains_results = sparkpost('GET', 'sending-domains', [], $headers);
echo "Sending domain results:\n";
echo json_encode(json_decode($sending_domains_results, false), JSON_PRETTY_PRINT);
echo "\n"; |
The script is working however I've received error in a response. Sending email... Email results: { "errors": [ { "message": "Message generation rejected", "description": "Sending domain sandbox option mismatch", "code": "1902" } ], "results": { "total_rejected_recipients": 0, "total_accepted_recipients": 1, "id": "48533314143036926" } } Listing sending domains... Sending domain results: { "results": [ { "shared_with_subaccounts": false, "domain": "yuppi.com.ua", "status": { "ownership_verified": true, "spf_status": "unverified", "abuse_at_status": "unverified", "compliance_status": "valid", "dkim_status": "valid", "verification_mailbox_status": "unverified", "postmaster_at_status": "unverified" } }, { "shared_with_subaccounts": false, "domain": "yuppiannunci.it", "status": { "ownership_verified": true, "spf_status": "unverified", "abuse_at_status": "unverified", "compliance_status": "valid", "dkim_status": "valid", "verification_mailbox_status": "unverified", "postmaster_at_status": "unverified" } } ] } |
if to set sandbox=false it is working. Thank you very much!) |
Yeah, you can rip out the |
Thank you so much, for this Awesome Help. It's helping me so much to understand how the API should be coded. Awesome job! Thanks again, for the sharing |
Hi there! I have pretty old PHP application without namespaces and even without composer. Is there a way I can use this library without composer?
The text was updated successfully, but these errors were encountered: