Get incoming webhook of woocommerce - php

I'm trying to receive a JSON of a woocommerce webhook in a tracker.php to manipulate the content, but something is wrong because it doesn't save anything in $_SESSION. This is my code ....
(!isset($_SESSION))? session_start() : null;
if($json = json_decode(file_get_contents("php://input"), true)) {
$data = json_decode($json, true);
$_SESSION["json"] = $data;
} else {
var_dump($_SESSION["json"]);
}
tested the webhook with http://requestbin.fullcontact.com/ and received the content. here a capture

issue is in this line
$data = json_decode($json, true);
here $json is array and jsondecode expect string.
here is code which will work.
(!isset($_SESSION))? session_start() : null;
if($json = json_decode(file_get_contents("php://input"), true)) {
//this seection will execute if you post data.
$_SESSION["json"] = $json;
} else {
//this will execute if you do not post data
var_dump($_SESSION["json"]);
}

Related

can't get right api result from json (using json_decode)

I want to print result from API...
But it is not working... I don't know what is wrong I tried to do it but got this and it is not working:
<?php
$api = 'https://api.battlemetrics.com/servers/'; // the main API for servers.
$server = '5090469'; // the number of the server you will get the info from most of thime something like 5484856.
$api_full = $api . $server; // bringing them together.
$json = file_get_contents("$api_full"); // Putting the content of the file in a variable.
$response = json_decode($json, true); // decode the JSON feed
if ($response['data']['status'] == "online") {
echo "ONLINE";
} else {
echo "OFFLINE";
};
?>
Extra if you want to see the json response this is the link: https://api.battlemetrics.com/servers/5090469
Thanks in advance for your tips and help.
Looks like you are just missing a key in your if statement.
It should be:
if($response['data']['attributes']['status'] == "online")

JSON post method in php

I don't get what do I do wrong when I try to get my values from post method from angular to php, it seems that everything is correct but then I am not able to see any values to postman/return the correctly.
this is my php method with all my attempts:
public function created()
{
$json = file_get_contents('php://input');
$json_string_in_array = array($json);
$json_array =json_decode($json_string_in_array[0]);
var_dump($json_string_in_array);
var_dump($json_array);
//return $json_array;
// $data = json_decode(file_get_contents('php://input'), true);
// $data = json_encode(file_get_contents("php://input"));
// $request = json_decode($data);
// var_dump($data);
// return $data;
// return $request;
// $json = file_get_contents('php://input');
//$obj = json_decode($json);
// get the raw POST data
// $rawData = file_get_contents("php://input");
// this returns null if not valid json
// return response()->json($rawData);
// DatabaseModel::DatabaseModel($json_array);
}
what I am trying to do is get that data and then send it to DatabaseModel in order to add it to database.
First you header, content-type, should be application/json. Are you manually setting the headers in angular? I am pretty sure angular will detect (or at least it is the default) the content type if its not set so in your case it would be application/json
<?php
$json = file_get_contents('php://input');
$request = json_decode($json);
$name = $request->name;
$email = $request->email;
# etc
?>

Php curl showing empty string after data

Ive got this little snippet that I use to get data about what players are online in my server. The data displays correctly, I just have 1 extra empty row being echoed.
<?php
$id = ***; //service ID
$api_key = '***'; //CBSM API key
$act = 5; //action #5 indicates retrieval of online players
$response = null;
$ch = curl_init('https://api.envul.com/?s='.$id.'&api='.$api_key.'&act='.$act);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if($response !== false) {
$response_data = explode("<hr>", $response); //process the data
foreach($response_data as $player_data)
{ // listing of players
$player_data = explode("{#!}", $player_data); //process the data
echo 'Username: '.$player_data[0].'<br>';
} //end listing of players
} else {
echo 'Error '.curl_error($ch);;
}
?>
Ive looked at countless posts on here and other sites about this issue with both curl, and file_get_contents.
Both methods return an empty string with and without actual data to display.

file_get_contents did not return the complete data in PHP

While parsing the below code, file_get_contents(url) returns only part of the data instead of the complete data. After clearing the browsing history and run the same code some more data is avaiable in $data3, but the complete data from the URL is still not retrieved.
Can you please suggest how to resolve this issue to retrieve all the data from the URL?
PHP code:
$results = file_get_contents('urllink');
$json_array = json_decode($results, true);
foreach ($json_array as $value) {
$key1 = $value["LineageId"];
$url = "urllink.$key1.apikey";
echo $url;
$data3 = file_get_contents($url);
echo $data3;
}

Using readability parser API to fetch image url from web page

Here I found readability which parse the data from web page and give information of interest.
But I could not understand how to use it.
https://www.readability.com/developers/api/parser
Request: GET /api/content/v1/parser?url=http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas/&token=1b830931777ac7c2ac954e9f0d67df437175e66e
It gives json response.
I am working on PHP, how to make above request for particular url like http://google.com?
UPDATE1:
<?php
define('TOKEN', "1b830931777ac7c2ac954e9f0d67df437175e66e");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
//$TOKEN = '1b830931777ac7c2ac954e9f0d67df437175e66e';
//Also tried with $API_URL = 'http://blog.readability.com/2011/02/step-up-be-heard-readability-ideas'; with no luck
// build our url
$url = sprintf(API_URL, $encodedUrl, TOKEN); //Also tried with $TOKEN
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
echo get_image('http://nextbigwhat.com/');
?>
Any issue with code? gives error - >
Warning: file_get_contents(https://www.readability.com/api/content/v1/parser?url=http%3A%2F%2Fnextbigwhat.com&token=1b830931777ac7c2ac954e9f0d67df437175e66e): failed to open stream: HTTP request failed! HTTP/1.1 403 FORBIDDEN in F:\wamp\www\inviteold\test2.php on line 14
Perhaps something like this:
define('TOKEN', "your_token_here");
define('API_URL', "https://www.readability.com/api/content/v1/parser?url=%s&token=%s");
function get_image($url) {
// sanitize it so we don't break our api url
$encodedUrl = urlencode($url);
// build our url
$url = sprintf(API_URL, $encodedUrl, $token);
// call the api
$response = file_get_contents($url);
if( $response ) {
return false;
}
$json = json_decode($response);
if(!isset($json['lead_image_url'])) {
return false;
}
return $json['lead_image_url'];
}
The code is not tested so you might need to adjust it.

Categories