How do I use received JSON with PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm reading an integration API and it says it will send requests in this format:
content-type: application/json
accept: application/json
x-gsc-api-key: lRDs8NfsVTmHD8SC1234
{
"name" : "testuser",
"password" : "098f6bcd4621d373cade4e832627b4f6",
"reference" : "b86de61-af20-4c47-af9a-6f2edeebc4fe"
}
I want to server the requests with PHP. I know some other posts also ask something along the same lines, but they were sending the request via AJAX method. I don't know which method the request is going to be sent.
Can I just reference the variables $_POST['name'] and $_POST['password'] to get the values? Or do I need to call json_decode($_POST)?
Is this information enough the write a PHP script that server the request?
Any help would be appreciated.

No, you cannot use $_POST directly. You will have to manually extract the response body, json_decode it and work on the results. But it's easy to do:
$data = json_decode(file_get_contents('php://input'));
echo $data->name;

$json = file_get_contents('php://stdin');

You have to use the json_decode() function for this (http://php.net/manual/en/function.json-decode.php)

No - you can't - however you can convert the JSON to $_POST like so:
<?php
$_POST = array_merge($_POST, json_decode(file_get_contents("php://stdin"), true));
?>

Not sure if i read the question correctly but >.>
$json = '{"name":"testuser","password":"098f6bcd4621d373cade4e832627b4f6","reference":"b86de61-af20-4c47-af9a-6f2edeebc4fe"}';
$data = #json_decode($json);
$name = $data->name;
$password = $data->password;
$ref = $data->reference;
echo "${name} ${password} ${ref}";
or use the true like below. . .
$json = '{"name":"testuser","password":"098f6bcd4621d373cade4e832627b4f6","reference":"b86de61-af20-4c47-af9a-6f2edeebc4fe"}';
$data = #json_decode($json, true);
$name = $data['name'];
$password = $data['password'];
$ref = $data['reference'];
echo "${name} ${password} ${ref}";

Related

Decoding Json string in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My Json encoded output is like the below one
{
"msg_id":"14789",
"message":"dummy+message",
"msgType":"TEXT",
"sendondate":"2013-12-26 13:19:49",
"seq_id":{
"1":{
"valid":"true",
"credit":"1.00",
"linecount":1,
"billcredit":1,
"id_provider":"18",
"providerkey":"TI",
"regionKey":"CH",
"originalnumber":"11",
"validnumber":"+11",
"countryprefix":"11",
"ONLYNUMBER":"11",
"NUMBERWITHZERO":"11",
"INTERNATIONALONLY":"11",
"INTERNATIONALWITHPLUS":"+11",
"mnpID":"905",
"dlr_seq":1,
"textMessage":"dummy+message",
"status":"",
"remarks":""
}
}
}
I want to print the value for billcredit as output. How can I decode this in php?
Try like this :
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'};
For more about json in php visit the link.
like this
var_dump(json_decode($json));
this is what you want to get with json_decode()
$string = '{"msg_id":"14789","message":"dummy+message","msgType":"TEXT","sendondate":"2013-12-26 13:19:49","seq_id":{"1":{"valid":"true","credit":"1.00","linecount":1,"billcredit":1,"id_provider":"18","providerkey":"TI","regionKey":"CH","originalnumber":"11","validnumber":"+11","countryprefix":"11","ONLYNUMBER":"11","NUMBERWITHZERO":"11","INTERNATIONALONLY":"11","INTERNATIONALWITHPLUS":"+11","mnpID":"905","dlr_seq":1,"textMessage":"dummy+message","status":"","remarks":""}}}';
// Return Object Data
print_r( json_decode($string) );
// Return Array Data
print_r( json_decode($string, true) );
$decoded_data = json_decode($string, true);
// Bill Credit Value
echo "billcredit: " . $decoded_data['seq_id'][1]['billcredit'];
http://codepad.org/BJ5KbHVq
$your_data_in_array_comes_here = json_decode("Your Output String");
With above array you can generate html as your wish.

Scraping & Returning JSON data php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I currently have a script, Can be found here:
http://ddelay.co.uk/bus/find_services2.php
With the following code:
<?php
include('simple_html_dom.php');
$service = $_GET["stop"];
$debug = $_GET["debug"];
$url = $service;
if($debug === "yes"){
echo "URL IS: " . $url . "\n";
}
$search = array('?', ' ', '.asp&');
$replace = array('&', '+', '.asp?');
$url2 = str_replace($search, $replace, $url);
if($debug === "yes"){
echo "REPLACEMENTS: ". $url2 . "\n";
}
$end = "http://tsy.acislive.com" . $url2 . '&showall=1';
if($debug === "yes"){
echo "FINAL URL: ". $end;
}
$html = file_get_html($end);
$ret = $html-> getElementsByTagName('table');
print($ret);
?>
For example which will pull the table from tsy.acislive.com (example: http://ddelay.co.uk/bus/find_services2.php?stop=/web/public_service_stops.asp?service=22?operatorid=36?systemid=30?goingto=Woodhouse)
I then want to be able to convert this table to JSON data to use in my app. Unfortunately I have tried PHP's function JSON_encode($ret); but unfortunately that failed. Would anybody know of how I can convert this table pulled using Simple-Dom-Parser php into Json Data
If you want to convert to JSON, json_encode is the way you should do it. It's a really easy to use function. If you're having trouble with it, there'll be an underlying reason.
Try these to find out what your data looks like:
var_dump($html);
var_dump($ret);
From the PHP manual, the value passed to json_encode Can be any type except a resource., so if it's failing I can only assume that you aren't passing it the correct data.
Post the results of those var_dump calls and I'll edit this with a solution for you.

How to get Magento Store Details in a PHP Array by Store ID? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a function which takes store id and return a PHP array which have the store details like store name, store code, logo, banner, name etc.
you can get a store details like this:
$store = Mage::getModel('core/store')->load($storeId);
$code = $store->getCode();
$name = $store->getName();
You can do this to see what data you can get from the store object
var_dump($store->getData())
The logo and other settings you need to get from the config section.
$logo = Mage::getStoreConfig('design/header/logo_src', $soreId);
This way you can get all the information from the config. You just need the correct path. For this you can see the name of the input field from system->configuration and section name and build the path.
Let's analyse the logo. You can find it in the Design tab and the url looks like this: 'admin/system_config/edit/section/design'. So the first part of the path is the section name design.
The field name is groups[header][fields][logo_src][value]. Just remove groups, [fields] and [value] and you get the rest of the path header/logo_src.
Try this it will work...
public function get_storedetails($store) {
$res = array();
try {
$res["store"] = Mage::app()->getStore($store);
Mage::app()->setCurrentStore($store);
$res["storeid"] = Mage::app()->getStore($store)->getStoreId();
$res["storecode"] = Mage::app()->getStore($store)->getCode();
$res["storewebid"] = Mage::app()->getStore($store)->getWebsiteId();
$res["storename"] = Mage::app()->getStore($store)->getName();
$res["storeactive"] = Mage::app()->getStore($store)->getIsActive();
$res["rooturl"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$res["storeurl"] = Mage::helper('core/url')->getHomeUrl();
$res["storelogo_alt"] = Mage::getStoreConfig('design/header/logo_alt');
$res["storefrontname"] = Mage::app()->getStore($store)->getFrontendName(); //getLogoSrc()
$res["current_url"] = Mage::helper('core/url')->getCurrentUrl();
$res["media_url1"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$res["media_url2"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
$res["skin_url"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
$res["js_url"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
$res["storelogo"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'frontend/default/default/'.Mage::getStoreConfig('design/header/logo_src');
$res["storeadminname"] = Mage::getStoreConfig('trans_email/ident_sales/name');
$res["storeemail"] = Mage::getStoreConfig('trans_email/ident_sales/email');
}
catch(Exception $ex) {
echo $ex;
}
return $res;
}

Str replace function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to censor words from a form string, and control the censor words with sql database. Here's what I have so far:
while ($bad_w = mysql_fetch_array($result_badw)) {
$newmessage = str_replace($bad_w['word'],"****",$org);
}
Any ideas on how to correct it?
You are repeatedly using the original string in your replacement. You should be overwriting the string
($org = str_replace($bad_w['word'],"****",$org)) to ensure all words are filtered.
Just hope nobody talks about Kuroshitsuji on your forum :p
You overwrite $newmessage each time you are replacing new word.
You should also use str_ireplace(), which is case-insensitive - that will be better for censorship.
Try like this:
$newmessage = $org;
while($row = mysql_fetch_array($result_badw)) {
$newmessage = str_ireplace($row['word'], "****", $newmessage);
}
Or, if you want the same number of * as are letters in the bad word:
$newmessage = $org;
while($row = mysql_fetch_array($result_badw)) {
$bword = $row['word'];
$repl = str_repeat('*', strlen($bword));
$newmessage = str_ireplace($bword, $repl, $newmessage);
}

Writing array to a file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In the program below
$array[name1][0] = 'name';
$array[name1][1] = '11';
$array[name2][0] = 'name2';
$array[name2][1] = '11';
$fileName = "file.php"
$fp = fopen($fileName,'w');
$msg = $array;
fwrite($fp,$msg);
fclose($fp);
In this file "file.php", i want to write an array...such tha its read like
<?
$array[name1][0] = 'name';
$array[name1][1] = '11';
$array[name2][0] = 'name2';
$array[name2][1] = '11';
but it's not working.
Use json_encode or serialize to get a storable (and compact) representation of your data structure, then json_decode or unserialize to get it back.
The var_export function does just that:
fprintf($fp, '<?php $array = %s;', var_export($array, true));
It generates valid PHP code and you can include the file after that:
include "file.php";
Note that you can return from a PHP file, so this would work too:
fprintf($fp, '<?php return %s;', var_export($array, true));
And then:
$array = include "file.php";
Alternatives to generating PHP code are json_encode/json_decode, or serialize/unserialize.
You can do it like this:
fwrite($fp,print_r($msg,true));
If you want to be able to read the file later and get an PHP array back, then you need to "serialize" the array:
and later
$array = unserialize(file_get_contents("test.data"));
if it should look readable in the file, you var_export($array,1) or print_r($array,1) and store their output.
The closest you're going to get, natively, is by using var_export and writing its return value to the file.
Failing that, you should implement something to build that format from an Array.

Categories