Home PHP Curl to API
Reply: 0

PHP Curl to API

user2572 Published in May 21, 2018, 12:34 pm

I'm trying to write a custom web interface for my wifi pineapple nano. I am not an experienced coder or anything, but I know and somewhat understand php and decided to use php curl to talk to the build in REST API on the pineapple.

I've written the following code:

print "your code started</br>";


//define and encode json
/*$a = new \stdClass();

        'message'=>'Hello from apache!',

    $JSON = json_encode($a);
    print $JSON;
    //curl post request
    $c = curl_init();
    curl_setopt($c, CURLOPT_CUSTOMREQUEST, "POST");
    //curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c, CURLOPT_URL, "");
    curl_setopt($c, CURLOPT_POST, true);
    curl_setopt($c, CURLOPT_POSTFIELDS, $JSON);
    curl_setopt($c, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Authorization: ' . $key

    //print curl output
    $result = curl_exec($c);
    print $result;
    $info = curl_getinfo($c);
print '</br>';
print $info['http_code'];
        die('Error: "' . curl_error($c) . '" - Code: ' . curl_errno($c));
//close curl request
print "</br>Your code ran";

On execution I get the following output:

your code started {"system":"notifications","action":"addNotification","message":"Hello from apache!","apiToken":"7de64925dd8703fc5595a7d0b845ce0ed965fa799b67a19b310cb2538017aeaacb205e6dbc72d8758c3b995d25a1a273783c49bfb7519b2e71fbd49af074ab35"} 200Error: "" - Code: 0

However, on the pineapple there's nothing happening. When I look at the data through wireshark, I get a lot of ' 301 moved permanently' packages.

Anyone know the answer to this, or maybe a better way of doing what I am trying to do?

Thanks in advance!

FYI: The curl request works in both CLI and postman, there doesn't seem to be any problem using these methods.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.393962 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO