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

Closed
CodeEsthete opened this Issue Mar 28, 2017 · 11 comments

Comments

Projects
None yet
3 participants
@CodeEsthete
Copy link

CodeEsthete commented Mar 28, 2017

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?

@avigoldman

This comment has been minimized.

Copy link
Member

avigoldman commented Mar 28, 2017

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.

@CodeEsthete

This comment has been minimized.

Copy link
Author

CodeEsthete commented Mar 28, 2017

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.

@CodeEsthete

This comment has been minimized.

Copy link
Author

CodeEsthete commented Mar 28, 2017

Thanks for your replay. Is there a guide how to use SparkPost API without the library from PHP?

@avigoldman

This comment has been minimized.

Copy link
Member

avigoldman commented Mar 28, 2017

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.

@avigoldman

This comment has been minimized.

Copy link
Member

avigoldman commented Mar 28, 2017

I think I have something that uses the SparkPost API with curl somewhere, one sec :)

@CodeEsthete

This comment has been minimized.

Copy link
Author

CodeEsthete commented Mar 28, 2017

Thanks, I appreciate.

@avigoldman

This comment has been minimized.

Copy link
Member

avigoldman commented Mar 28, 2017

@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' => 'from@sparkpostbox.com',
        ],
        'subject' => 'Sending an email with SparkPost',
        'html' => '<html><body><h1>Sending mail with PHP+SparkPost+cURL!</h1></body></html>',
    ],
    'recipients' => [
        [ 'address' => 'email@example.com', ],
    ],
];

$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";
@CodeEsthete

This comment has been minimized.

Copy link
Author

CodeEsthete commented Mar 28, 2017

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" } } ] }

@CodeEsthete

This comment has been minimized.

Copy link
Author

CodeEsthete commented Mar 28, 2017

if to set sandbox=false it is working. Thank you very much!)

@avigoldman

This comment has been minimized.

Copy link
Member

avigoldman commented Mar 28, 2017

Yeah, you can rip out the "sandbox" => true if you are not using the sparkpostbox.com sending domain. I'm gonna go ahead and close this issue. Feel free to shout if you hit any other problems, or ask in our community slack.

@avigoldman avigoldman closed this Mar 28, 2017

@fernandoaurelio

This comment has been minimized.

Copy link

fernandoaurelio commented Nov 6, 2018

@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' => 'from@sparkpostbox.com',
        ],
        'subject' => 'Sending an email with SparkPost',
        'html' => '<html><body><h1>Sending mail with PHP+SparkPost+cURL!</h1></body></html>',
    ],
    'recipients' => [
        [ 'address' => 'email@example.com', ],
    ],
];

$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";

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment