简体   繁体   中英

trying to do curl post request using php

I know this isn't new, using php to do a post request using curl, but I am battling with this one. The curl command line input which does work sucessfully is as follows:

curl -v  -X POST --data  "{\"dev_id\":\"a8610a3237397a01\",\"payload_raw\":\"Ag==\"}"  https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/mkrwan1300a8610a3237397a01/sendvalvestatus?key=ttn-account-v2.lYA16-xxxxxxxxxxxxxxxxxxxxxx

And I am trying to do the equivalent in PHP so as to be able to action a downlink to my endpoint in things network. I am trying the following:

<?php


 echo('hello');

 $url = 'https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/mkrwan1300a8610a3237397a01/sendvalvestatus?key=ttn-account-v2.lYA16--xxxxxxxxxxxxxxxxxxxxxx';
    $ch = curl_init($url);  
     // Setup request to send json via POST
    $data = array(
        'AQ=='
    );
    $payload = json_encode(array("payload_raw" => 'AQ=='));

    // Attach encoded JSON string to the POST fields
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    // Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

    // Return response instead of outputting
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute the POST request
    $result = curl_exec($ch);

 
    $output=curl_exec($ch);
    $info = curl_getinfo($ch);
    echo "<pre>";   
    print_r($info);
    echo "</pre>";    
     // Close cURL resource
    curl_close($ch);

 
?>

The result I get is:

hello
Array
(
    [url] => https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/mkrwan1300a8610a3237397a01/sendvalvestatus?key=ttn-account-v2.lYA16--xxxxxxxxxxxxxxxxxxxxxx
    [content_type] => text/plain; charset=utf-8
    [http_code] => 400
    [header_size] => 171
    [request_size] => 270
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.203
    [namelookup_time] => 1.0E-6
    [connect_time] => 1.0E-6
    [pretransfer_time] => 1.0E-6
    [size_upload] => 22
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 108
    [download_content_length] => 0
    [upload_content_length] => 22
    [starttransfer_time] => 0.203
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 13.69.184.129
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => 10.0.0.109
    [local_port] => 53234
)

so it isn't happy with my data. I'm just trying to send a payload_raw in base64 encoding of the value: 'AQ==' Any ideas anyone?

Ok, so I forgot to include he device ID as part of the payload. Solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM