Skip to content

Instantly share code, notes, and snippets.

@daveh
Created August 8, 2021 15:10
Show Gist options
  • Select an option

  • Save daveh/c61fbe671894a7931d88a5ca82734921 to your computer and use it in GitHub Desktop.

Select an option

Save daveh/c61fbe671894a7931d88a5ca82734921 to your computer and use it in GitHub Desktop.

Revisions

  1. daveh created this gist Aug 8, 2021.
    23 changes: 23 additions & 0 deletions 1-file_get_contents.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    <?php

    $payload = json_encode([
    "title" => "Updated title"
    ]);

    $options = [
    "http" => [
    "method" => "PATCH",
    "header" => "Content-type: application/json; charset=UTF-8\r\n" .
    "Accept-language: en",
    "content" => $payload
    ]
    ];

    $context = stream_context_create($options);

    $data = file_get_contents("https://jsonplaceholder.typicode.com/albums/1", false,
    $context);

    var_dump($data);

    print_r($http_response_header);
    35 changes: 35 additions & 0 deletions 2-curl.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php

    $payload = json_encode([
    "title" => "Updated title"
    ]);

    $headers = [
    "Content-type: application/json; charset=UTF-8",
    "Accept-language: en"
    ];

    $ch = curl_init();

    /*
    curl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/albums/1");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    */
    curl_setopt_array($ch, [
    CURLOPT_URL => "https://jsonplaceholder.typicode.com/albums/1",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "PATCH",
    CURLOPT_POSTFIELDS => $payload,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_HEADER => true
    ]);

    $data = curl_exec($ch);

    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    var_dump($status_code);

    var_dump($data);
    24 changes: 24 additions & 0 deletions 3-guzzle.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php

    require __DIR__ . "/vendor/autoload.php";

    $payload = json_encode([
    "title" => "Updated title"
    ]);

    $headers = [
    "Content-type" => "application/json; charset=UTF-8"
    ];

    $client = new GuzzleHttp\Client();

    $response = $client->patch("https://jsonplaceholder.typicode.com/albums/1", [
    "headers" => $headers,
    "body" => $payload
    ]);

    var_dump($response->getStatusCode());

    var_dump($response->getHeader("Content-type"));

    var_dump((string) $response->getBody());
    33 changes: 33 additions & 0 deletions 4-sdks.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php

    $api_key = "your api key here";

    $data = [
    'name' => 'Bob',
    'email' => '[email protected]'
    ];

    /*
    $ch = curl_init();
    curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.stripe.com/v1/customers',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_USERPWD => $api_key
    ]);
    $data = curl_exec($ch);
    curl_close($ch);
    var_dump($data);
    */

    require __DIR__ . "/vendor/autoload.php";

    $stripe = new \Stripe\StripeClient($api_key);

    $customer = $stripe->customers->create($data);

    echo $customer;