Skip to content

How to use API without composer #164

Closed

Description

@pitometsyurii

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?

Activity

  1. avigoldman commented on Mar 28, 2017

    @avigoldman
    Contributor

    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.

  2. pitometsyurii commented on Mar 28, 2017

    @pitometsyurii
    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.

  3. pitometsyurii commented on Mar 28, 2017

    @pitometsyurii
    Author

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

  4. avigoldman commented on Mar 28, 2017

    @avigoldman
    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.

  5. avigoldman commented on Mar 28, 2017

    @avigoldman
    Contributor

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

  6. pitometsyurii commented on Mar 28, 2017

    @pitometsyurii
    Author

    Thanks, I appreciate.

  7. avigoldman commented on Mar 28, 2017

    @avigoldman
    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";
  8. pitometsyurii commented on Mar 28, 2017

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

  9. pitometsyurii commented on Mar 28, 2017

    @pitometsyurii
    Author

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

  10. avigoldman commented on Mar 28, 2017

    @avigoldman
    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.

  11. fernandoaurelio commented on Nov 6, 2018

    @fernandoaurelio

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions