Home PHP Curl to API
Reply: 0

PHP Curl to API

user2572
1#
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:

?php
print "your code started</br>";

$key="7de64925dd8703fc5595a7d0b845ce0ed965fa799b67a19b310cb2538017aeaacb205e6dbc72d8758c3b995d25a1a273783c49bfb7519b2e71fbd49af074ab35";

//define and encode json
/*$a = new \stdClass();
$a->module="Filters";
$a->action="addClient";
$a->mac="c0:ee:fb:4a:c1:57";
$a->apiToken=$key;
 */

$a=[
        'system'=>'notifications',
        'action'=>'addNotification',
        'message'=>'Hello from apache!',
        'apiToken'=>'7de64925dd8703fc5595a7d0b845ce0ed965fa799b67a19b310cb2538017aeaacb205e6dbc72d8758c3b995d25a1a273783c49bfb7519b2e71fbd49af074ab35'

   ];
    $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, "172.16.42.1/api");
    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'];
if(!curl_exec($c)){
        die('Error: "' . curl_error($c) . '" - Code: ' . curl_errno($c));
}
//close curl request
        curl_close($c);
        //debug
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