I want only a specific value(say "name") from my generated output. I used this code
var_dump($request_object_details); to generate the below output.
What output I am getting:-
array(6) { ["id"]=> string(26) "514484461930096_1446926141" ["application"]=> array(2) { ["name"]=> string(18) "Pocket Financetest" ["id"]=> string(15) "270339389736782" } ["to"]=> array(2) { ["name"]=> string(12) "Prince Singh" ["id"]=> string(10) "1446926141" } ["from"]=> array(2) { ["name"]=> string(14) "Ashutosh Singh" ["id"]=> string(15) "100003645579131" } ["message"]=> string(16) "My Great Request" ["created_time"]=> string(24) "2013-01-17T03:45:36+0000" }
I used the below code to get the above output:
<?php
require_once('phps/fbsdk/src/facebook.php');
$config = array
(
'appId' => '270339389736782',
'secret' => '667e1795cc1f308f312d49d6b1c17cb8',
);
$facebook = new Facebook($config);
//get the request ids from the query parameter
$request_ids = explode(',', $_REQUEST['request_ids']);
//build the full_request_id from request_id and user_id
function build_full_request_id($request_id, $user_id) {
return $request_id . '_' . $user_id;
}
//for each request_id, build the full_request_id and delete request
foreach ($request_ids as $request_id)
{
$uid = $facebook->getUser();
$full_request_id = build_full_request_id($request_id, $uid);
$request_object_details = $facebook->api("/$full_request_id");
var_dump($request_object_details);//This is giving me the output
try {
$delete_success = $facebook->api("/$full_request_id",'DELETE');
if ($delete_success) {
echo "Successfully deleted " . $full_request_id;}
else {
echo "Delete failed".$full_request_id;}
}
catch (FacebookApiException $e) {
echo "error=".$e;}
}
?>
Your question is not clear but still I am posting some sort of code for you.
Use json_decode
Do like this : It will return object
$obj = json_decode($request_object_details);
$result= $obj->id;
OR
You can use json_decode and convert it into the array:
$request_object_details = $facebook->api("/$full_request_id");
$result=json_decode($request_object_details, true);
The second parameter will make decoded json string into an associative arrays.
Now you can directly use :
echo $result['id'];
echo $result['application'];
and so on...
Hope it'll help you.
Related
So I am trying to integrate btcpayserver into my site. I am fairly new to php so bare with me but I have everything working until after the invoice is created. Whenever user clicks to pay with bitcoin, it successfully creates new invoice but doesn't redirect to the checkout link afterwards.
I should mention i am using the following to help with integration process https://github.com/btcpayserver/btcpayserver-greenfield-php
Here is my create_invoice.php
session_start();
// Include autoload file.
require __DIR__ . '/../vendor/autoload.php';
// Import Invoice client class.
use BTCPayServer\Client\Invoice;
use BTCPayServer\Client\InvoiceCheckoutOptions;
use BTCPayServer\Util\PreciseNumber;
// Fill in with your BTCPay Server data.
$apiKey = 'aaaaaaaaaa';
$host = 'https://aa.demo.btcpayserver.org/';
$storeId = 'aaaaaa';
$amount = $_POST['amount'];
$currency = 'USD';
$orderId = $_SESSION['name']."#" . mt_rand(0, 1000);
$buyerEmail = $_SESSION['name'];
try {
$client = new Invoice($host, $apiKey);
var_dump(
$client->createInvoice(
$storeId,
$currency,
PreciseNumber::parseString($amount),
$orderId,
$buyerEmail
)
);
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage();
}
Whenever the form is submitted the following is shown (i replaced the sensitive information)
object(BTCPayServer\Result\Invoice)#6 (1)
{
["data":"BTCPayServer\Result\AbstractResult":private] => array(16)
{
["id"]=> string(22) "aaaaaaaaaaa"
["storeId"]=> string(44) "ssssssssssssssssssssssssssss"
["amount"]=> string(4) "18.0"
["checkoutLink"]=> string(62) "https://demo.aa.btcpayserver.org/i/aaaaaaaaaaaaaa"
["status"]=> string(3) "New"
["additionalStatus"]=> string(4) "None"
["monitoringExpiration"]=> int(1669087659)
["expirationTime"]=> int(1669080459)
["createdTime"]=> int(1669078659)
["availableStatusesForManualMarking"]=> array(2)
{
[0]=> string(7) "Settled"
[1]=> string(7) "Invalid"
}
["archived"]=> bool(false)
["type"]=> string(8) "Standard"
["currency"]=> string(3) "USD"
["metadata"]=> array(1)
{
["orderId"]=> string(4) "#501"
}
["checkout"]=> array(10) {
["speedPolicy"]=> string(9) "HighSpeed"
["paymentMethods"]=> array(1)
{
[0]=> string(3) "BTC"
}
["defaultPaymentMethod"]=> NULL
["expirationMinutes"]=> int(30)
["monitoringMinutes"]=> int(120)
["paymentTolerance"]=> float(0)
["requiresRefundEmail"]=> NULL
["defaultLanguage"]=> NULL
}
["receipt"]=> array(3)
{
["enabled"]=> NULL
["showQR"]=> NULL
["showPayments"]=> NULL
}
}
}
Now my issue is I need to redirect to "checkoutLink" after invoice is created.
UPDATE*
Following code displays error "Error: Cannot access offset of type string on string"
if ($response->getStatus() === 200) {
$data1 = $response->getBody();
$link1 = $data1["checkoutLink"];
echo $link1;
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
My request PHP file elaborates some Ajax POST data:
POST data
data[0][id]:359
data[0][position]:1
data[1][id]:321
data[1][position]:2
data[2][id]:354
data[2][position]:3
Request.php
if(isset($_POST['data'])) {
if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
$verify = $loggedIn->verify();
if($verify['username']) {
$Profile = new Profile();
$Profile->db = $db;
//Call my function
$messages = $Profile->setOrder($_POST['data']);
}
}
}
Profile.php
function setOrder($post) {
var_dump($post);
foreach($post as $item)
{
return "Area ID ".$item["id"]." and person located ".$item["position"]."<br />";
}
}
My function returns nothing and the dump of $post is as below
array(3) {
[0]=>
array(2) {
["id"]=>
string(3) "359"
["position"]=>
string(1) "1"
}
[1]=>
array(2) {
["id"]=>
string(3) "321"
["position"]=>
string(1) "2"
}
[2]=>
array(2) {
["id"]=>
string(3) "354"
["position"]=>
string(1) "3"
}
}
Inside my function I can dump correctly something like var_dump($post[0]["id"]); so why my foreach loop is empty?
It is because you are using return inside loop. It will terminate the loop after first iteration. You need to do something like this.
$return = null;
foreach($data as $item)
{
$return .= "Area ID ".$item["id"]." and person located ".$item["position"]."<br />";
}
return $return;
I need to get the objects information for "label", "name" where value=true in a PHP variable and not were value=false.
How is this done with this JSON array?
If I make a var_dump of the JSON I get this:
array(8) {
[0]=>
object(stdClass)#8 (3) {
["label"]=>
string(4) "Name"
["name"]=>
string(7) "txtName"
["value"]=>
bool(true)
}
[1]=>
object(stdClass)#9 (3) {
["label"]=>
string(6) "E-mail"
["name"]=>
string(8) "txtEmail"
["value"]=>
bool(true)
}
[2]=>
object(stdClass)#10 (3) {
["label"]=>
string(12) "Phone Number"
["name"]=>
string(8) "txtPhone"
["value"]=>
bool(false)
}
[3]=>
object(stdClass)#11 (3) {
["label"]=>
string(19) "Mobile Phone Number"
["name"]=>
string(14) "txtMobilePhone"
["value"]=>
bool(false)
}
}
$arr = array();
$i = 0;
foreach($json as $key => $items) {
if($items->value == true) {
$arr[$i]['label'] = $items->label;
$arr[$i]['name'] = $items->name;
$i++;
}
}
You can decode it as an object or an array, in this example I use an array.
First you want to take the JSON encoded information and decode it into a PHP array, you can use json_decode() for this:
$data = json_decode($thejson,true);
//the Boolean argument is to have the function return an array rather than an object
Then you can loop through it as you would a normal array, and build a new array containing only elements where 'value' matches your needs:
foreach($data as $item) {
if($item['value'] == true) {
$result[] = $item;
}
}
You then have the array
$result
at your disposal.
Simplification of the suggestions proposed by users JohnnyFaldo and som:
$data = json_decode($thejson, true);
$result = array_filter($data, function($row) {
return $row['value'] == true;
});
I have been trying to read a friends news feed to check if my message is there (does not matter if the message is visible, hidden or deleted) and if the message is there, not to post a new message. If it is not there than post a message. The code is below with a lot of unnecessary code to trace where the problem was coming from. I have my permission set for 'publish_stream, read_stream'.
I tried all the code in the other blogs but non seemed to work I do not know whether it is because I am using PHP SDK 3.0 - latest version. My App is iframe app. Even bought an expensive book (too expensive) on Facebook Applications but found out, after buying, that it was out of date!
The code works well if I am using /me or my user id but not my friend's id.
require 'config.php';
require 'facebook.php';
//Create facebook application instance.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret
));
$sent = false;
$userData = null;
$PPnotposted = true;
$me = null;
$test_friends =1;
$user = $facebook->getUser();
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array("next"=> "<? echo $fb_app_url ?>" ));
} else {
$loginUrl = $facebook->getLoginUrl( array( 'scope'=> 'publish_stream, read_stream', 'redirect_uri' => $fb_app_url));
}
$get_someone_else = 100000xxxxxxxxx; // I used a real user id but covered it up here so
my friend's id hidden for this stackexchange help
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
if ( $get_someone_else ){
$user_profile = $facebook->api("/".$get_someone_else);
echo "<br/> ********************user is ".$get_someone_else." ******************<br/>";
var_dump( $user_profile );
echo "<br/> ********************** end of user info ******************<br/>";
} else {
$user_profile = $facebook->api('/me');
}
} catch (FacebookApiException $e) {
}
// print_r( $user_profile );
$PPaccesstoken = $facebook->getAccessToken();
$attachment[access_token]= $PPaccesstoken ;
//post message to wall if it is sent through form
if(isset($_POST['PPmessage'])){
try {
if( $get_someone_else ){
$ppmy_news = $facebook->api("/.$get_someone_else./feed?limit=300"); //Get 300 messages to examine
echo "<br/> *********************** news feed is ".$ppmy_news." ******************<br/>";
var_dump( $ppmy_news );
echo "<br/> ******************** end of user info ********************<br/>";
} else {
$ppmy_news = $facebook->api('/me/feed?limit=300'); //Get 300 messages
//to examine. Initially I set it to
// 10 messages for testing purposes.
}
// var_dump( $ppmy_news);
// $sent = $facebook->api("/".$user."/feed", 'POST', $attachment );
$ppmy_view_lp=0;
if( $test_friends ) { //Debugging flag to activate or deactivate parts of code
echo "sent is :<br />";
// var_dump( $sent );
echo "<br />";
$friends = $facebook->api('/me/friends');
echo "friends is :<br />";
// var_dump( $friends );
echo "<br />";
$friendsLists = $facebook->api('/me/friends');
$total_friends = 0;
foreach ($friendsLists as $friends) {
foreach ($friends as $friend) {
$id = $friend['id'];
$name = $friend['name'];
$total_friends++; //Get total number of friends
echo "friend : ".$name." id is: ".$id." Total friends is : ".$total_friends." <br/> ";
}
}
}
} catch (FacebookApiException $e) {
echo "<br/> !!!!!!!!!!!! Error Dump Line 85 !!!!!!!!!!!!! <br/>";
var_dump( $e );
echo "<br/> !!!!!!!!!!!! End of Error Dump !!!!!!!!!!!!! <br/>";
//do something about it
}
}
}
But I find I am denied permission and the catch dump is:
!!!!!!!!!!!! Error Dump Line 85 !!!!!!!!!!!!!
object(FacebookApiException)#2 (7) { ["result:protected"]=> array(1) { ["error"]=> array(3) { ["message"]=> string(72) "(#803) Some of the aliases you requested do not exist: .100000xxxxxxxxx." ["type"]=> string(14) "OAuthException" ["code"]=> int(803) } } ["message:protected"]=> string(72) "(#803) Some of the aliases you requested do not exist: .100000xxxxxxxxx." ["string:private"]=> string(0) "" ["code:protected"]=> int(0) ["file:protected"]=> string(48) "/home/www/drpetersnews.com/vmt/base_facebook.php" ["line:protected"]=> int(1106) ["trace:private"]=> array(4) { [0]=> array(6) { ["file"]=> string(48) "/home/www/drpetersnews.com/vmt/base_facebook.php" ["line"]=> int(810) ["function"]=> string(17) "throwAPIException" ["class"]=> string(12) "BaseFacebook" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> array(1) { ["error"]=> array(3) { ["message"]=> string(72) "(#803) Some of the aliases you requested do not exist: .100000xxxxxxxxx." ["type"]=> string(14) "OAuthException" ["code"]=> int(803) } } } } [1]=> array(4) { ["function"]=> string(6) "_graph" ["class"]=> string(12) "BaseFacebook" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(32) "/.100000xxxxxxxxx./feed?limit=10" } } [2]=> array(4) { ["file"]=> string(48) "/home/www/drpetersnews.com/vmt/base_facebook.php" ["line"]=> int(587) ["function"]=> string(20) "call_user_func_array" ["args"]=> array(2) { [0]=> array(2) { [0]=> &object(Facebook)#1 (7) { ["appId:protected"]=> string(15) "368XXXXXXXXXX09" ["appSecret:protected"]=> string(32) "eb4216aXXXXXXXXXXXXXXXXXXXXXfe21" ["user:protected"]=> string(15) "1000000yyyyyyyy" ["signedRequest:protected"]=> NULL ["state:protected"]=> NULL ["accessToken:protected"]=> string(119) "AAAFO5a2SKXkBAAXYgZBdPfTbJKQ3XpBgZBQmU2QcXOx7eo18hB5Hox41J7Of7SMZCj7ZCchJrM1ZCN2KLoAoqCmQWUrjwhDJlMQca005w5kgJIrMQ6ZCdM" ["fileUploadSupport:protected"]=> bool(false) } [1]=> string(6) "_graph" } [1]=> array(1) { [0]=> string(32) "/.100000xxxxxxxxx./feed?limit=10" } } } [3]=> array(6) { ["file"]=> string(40) "/home/www/drpetersnews.com/vmt/index.php" ["line"]=> int(54) ["function"]=> string(3) "api" ["class"]=> string(12) "BaseFacebook" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(32) "/.100000xxxxxxxxx./feed?limit=10" } } } }
!!!!!!!!!!!! End of Error Dump !!!!!!!!!!!!!
Please do help me out and let me know if I have set the permissions wrongly and how to do it properly.
The code is correct, but the problem could lie with Facebook's Privacy Settings. If you can't see the posts on the user's wall, it means that the user has set privacy settings preventing you from seeing the posts.
Use the Graph API Explorer tool (https://developers.facebook.com/tools/explorer/) and see if you can see the user's feed using it. Check a number of friend IDs to see if any of them return data.
I am calling a webservice and I am getting a complex object back. I need to display variables reg_no, opening_inventory_weight.
RESULT:-
object(stdClass)#12 (3) {
["TxnErrors"]=> object(stdClass)#13 (0) { }
["TxnStatus"]=> bool(true)
["headers"]=> object(stdClass)#14 (1) {
["RPMHeader"]=> array(1) {
[0]=> object(stdClass)#15 (7) {
["opening_inventory_weight"]=> int(1001)
["prepared_by"]=> string(5) "James"
["reg_no"]=> string(7) "5000005"
["reporting_period"]=> string(19) "2010-02-01T00:00:00"
["rsid"]=> int(49) ["status"]=> string(1) "D"
["web_user_id"]=> string(1) "0" } } } }
I am calling it like
$result = call_search_existing_manufacturer();
$rows = array();
foreach ($result->RPMHeader as $data)
{
$rows[] = array(
$data->reg_no,
$data->opening_inventory_weight,
$data->status
);
}
But its not working. Any Idea what am I missing? Thank you in advance
I think it should be
$result = call_search_existing_manufacturer();
$rows = array();
foreach ($result->headers->RPMHeader as $data)
{
$rows[] = array(
$data->reg_no,
$data->opening_inventory_weight,
$data->status
);
}
Your result dump isn't esay too read, so I may be wrong, but it looks like RPMHeader is part of headers field, so you should access it like
$result->headers->RPMHeader