0

I have tried but no success. I tried also different options but response is throwing 401 or there is no response. In API docs all I have is basic instructions, and as you may imagine I am new to cURL.

API Docs: https://docs.sendcloud.sc/api/v2/index.html?shell#get-all-parcels

Here is the code I have so far:

$url = "https://panel.sendcloud.sc/api/v2/parcels";

//  Initiate curl
$ch = curl_init();
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);


$arr = json_decode($result, true);

print_r($arr);

Thanks.

  • 1
    Did you read the docs you attached? Where are you sending the user? – Mike Doe Apr 23 '18 at 10:30
  • Possible duplicate of [How do I make a request using HTTP basic authentication with PHP curl?](https://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl) – Mike Doe Apr 23 '18 at 10:31
  • [To successfully access data that you require, you must include these API keys. Inside of every API request to our server. Through an HTTP header that would typically look the example on the right.](https://docs.sendcloud.sc/api/v2/index.html?shell#authentication) `--user "YOUR_API_KEY:YOUR_API_SECRET"`. – deEr. Apr 23 '18 at 10:34
  • @emix Yes, of course I have read docs, but as I mentioned in my question, I am pretty new to cURL. What I meant by that is that I don't know much about this subject. BTW thanks for suggestion. – Petar Ratković Apr 23 '18 at 10:40
  • This has more to do with logic, not cURL itself. As you can see by inspecting your code, you are simply trying to access the url. No wonder site responds with `401 unauthorized`. It basicly says "no way, I don't know who you are". Hence the duplicate closing request. – Mike Doe Apr 23 '18 at 12:18

1 Answers1

0
$credentials = base64_encode("publickey:secretkey");

    $requestBody = '{
        "parcel": {
            "name": "John Doe",
            "company_name": "FlowerShop",
            "email": "john@doe.com",
            "telephone": "+31611223344",
            "address": "Fürstenrieder Str.",
            "house_number": "70",
            "address_2": "",
            "city": "Munich",
            "country": "DE",
            "postal_code": "80686",
            "country_state": null,
            "customs_invoice_nr": "",
            "customs_shipment_type": null,
            "parcel_items": [
                {
                    "description": "T-Shirt",
                    "hs_code": "6109",
                    "origin_country": "SE",
                    "product_id": "898678671",
                    "properties": {
                        "color": "Blue",
                        "size": "Medium"
                    },
                    "quantity": 2,
                    "sku": "TST-OD2019-B620",
                    "value": "19.95",
                    "weight": "0.9"
                },
                {
                    "description": "Laptop",
                    "hs_code": "84713010",
                    "origin_country": "DE",
                    "product_id": "5756464758",
                    "properties": {
                        "color": "Black",
                        "internal_storage": "2TB"
                    },
                    "quantity": 1,
                    "sku": "LT-PN2020-B23",
                    "value": "876.97",
                    "weight": "1.69"
                }
            ],
            "weight": "3.49",
            "length": "31.5",
            "width": "27.2",
            "height": "12.7",
            "total_order_value": "896.92",
            "total_order_value_currency": "EUR",
            "shipment": {
                "id": 8,
                "name": "Unstamped letter"
            },
            "shipping_method_checkout_name": "Battery WarehouseX DHL",
            "sender_address": 317854,
            "quantity": 1,
            "total_insured_value": 0,
            "is_return": false,
            "request_label": false,
            "apply_shipping_rules": false,
            "request_label_async": false
        }
    }';

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://panel.sendcloud.sc/api/v2/parcels');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: Basic '.$credentials,
        'Content-Type: application/json',
    ));

    $response = curl_exec($ch);

    curl_close($ch);

    echo $response;