Start a new topic
Answered

Curl Push with PHP doesnt work

Hello everyone... 


first: sorry for my english...


I tried to push infos to my app with curl / php... curl is installed - if i modify the x-access-token i am getting an error... with the correct token, i am getting no response...


the code:

 

$url = "MY_PUSH_URL";

$frames = array( "frames" => array(
    array(
      'index' => 0,
      'text' => "Hello",
      'icon' => "i1653"
    )
  )
);

$headers = array(
    "Accept: application/json",
    "X-Access-Token: MY TOKEN", 
    "Cache-Control: no-cache", 
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($frames)); 
$response = curl_exec($curl);
curl_close($curl);

 

Whats wrong?


Best Answer

This code works for my app:


  

<?php
$url = "https://developer.lametric.com/api/v1/dev/widget/update/com.lametric.xxxxxx/2";

$frames = array(
	"frames" => array(
    		array(
                "index" => 0,
                "icon" => "i2486",
    			"text" => "Test Test Test"
    		),
            array(
                "index" => 1,
                "chartData" => array (
                4,3,2,1
                )
            )
)	
);

$curl = curl_init();

$headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
    "X-Access-Token: ACCESS_TOKEN_FROM_APP_PAGE", 
    "Cache-Control: no-cache", 
);

echo json_encode($frames);

echo "\n";

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($frames)); 


$response = curl_exec($curl);

echo "RESPONSE: " . $response;

curl_close($curl);

?>

 Empty response should be ok, if the request was successful.


THANK YOU - that works!



Answer

This code works for my app:


  

<?php
$url = "https://developer.lametric.com/api/v1/dev/widget/update/com.lametric.xxxxxx/2";

$frames = array(
	"frames" => array(
    		array(
                "index" => 0,
                "icon" => "i2486",
    			"text" => "Test Test Test"
    		),
            array(
                "index" => 1,
                "chartData" => array (
                4,3,2,1
                )
            )
)	
);

$curl = curl_init();

$headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
    "X-Access-Token: ACCESS_TOKEN_FROM_APP_PAGE", 
    "Cache-Control: no-cache", 
);

echo json_encode($frames);

echo "\n";

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($frames)); 


$response = curl_exec($curl);

echo "RESPONSE: " . $response;

curl_close($curl);

?>

 Empty response should be ok, if the request was successful.

No, not local... external server... 

I wrote a php script for my local home web server, I attach it, see if it helps. You can pass certain URL variables.


Be aware that it includes a file at start where it gets $apikey, $ip, $port from. You need to create that too

php

Are you operating inside your home network? If yes, you don't need to create an app, you can push directly to the LaMetric.

hmmm got an eMail that said "new replies" - something about the "api key"?!?!?!


the code above is my try for an indicator app - but doesnt work... found an tutorial that said, i need an api key and to get it i need the client id... but indicator apps doenst have client id's i think....


indicator pull app is working... but push app doenst work für me... 

Forgot one line: 


curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");


....but doenst work  :(

Login or Signup to post a comment