Skip to content
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
pitometsyurii opened this issue Mar 28, 2017 · 11 comments
Closed

How to use API without composer #164

pitometsyurii opened this issue Mar 28, 2017 · 11 comments

Comments

@pitometsyurii
Copy link

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
Copy link
Contributor

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.

@pitometsyurii
Copy link
Author

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.

@pitometsyurii
Copy link
Author

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

@avigoldman
Copy link
Contributor

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
Copy link
Contributor

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

@pitometsyurii
Copy link
Author

Thanks, I appreciate.

@avigoldman
Copy link
Contributor

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

@pitometsyurii
Copy link
Author

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

@pitometsyurii
Copy link
Author

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

@avigoldman
Copy link
Contributor

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.

@fernandoaurelio
Copy link

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants