I am trying to print some data which is in an array format. But while printing I am loosing my initial byte of the array which I need. How can I get that?
rray
(
[0] => stdClass Object
(
[company_category] => Company limited by Shares
[email_id] => example#domain.com
[last_processed] => 2016-07-01T08:42:34Z
[company_class] => Public
[country] =>
)
)
As you will be seeing that starting of the output should have be "Array" but it is not. What am I doing wrong here. As I am sending a request to a specific URL and getting a response from it. Below is the code I use to get the reponse
HttpEntity entity = CloseableHttpResponse.getEntity();
String response = EntityUtils.toString(entity,"UTF-8");
System.out.println(response);
Related
If You are sending an object to a webmethod to use it as prameter:
Example: sending an object that will be represented as a class in the server like this:
$creditCard = new stdClass();
$creditCard->number="705";
$creditCard->expiryDate="03/10/2019";
$creditCard->controlNumber=9;
$param = array("creditCard" => $creditCard);
$Response = $client->__soapCall('valider', array($param));
You won't get any property of the class CreditCard that is in the server to work, and every time you do creditClass.getNumber() or trying to use accessors you will get a null pointer error because your object is not really created.
To fix this problem you have to modify the server webservice and not the client: you have to add the trivial jax-ws annotation #webParam:
public String valider(#WebParam(name = "creditCard")CreditCard creditCard){ ... }
And your PHP soapClient code is very simple: (if you want more security you have to encode your credentials or use an other way for authentication like digest authentication or other jax ws security implementations)
try {
$opts = array (
'http' => [
'header' => "username: myrealname\r\n".
'password: pass1myrealpassword']
);
$par= array(
'trace' => 1,
'exceptions' =>0,
'soap_version' => SOAP_1_1,
'connection_timeout' => 1800,
'stream_context' => stream_context_create($opts),
);
$client = new SoapClient($wsdl,$par);
//...... and call your service method as you do
I am sending JSON Objects which contain converted ArrayLists. For the most part everything works fine except on the PHP page.
If I use:
if($_POST)
{
echo "Something was sent";
$obj = array();
$JSON_Entry = $_POST["Entry"];
$body = json_decode($JSON_Entry, true);
foreach ($body as $key => $value)
{
echo $value;
}
I get the response in android emulator logs;
Something was sent[SalesMade [id=0, product_description=Beer, qty=2, unit=3, total=6.0]]
But when I try to separate the array using:
foreach($value as $column => $row)
{
echo $row;
}
I get an Invalid argument supplied for foreach() error. Is it because I converted the ArrayList to a JSON object before posting?
First you have to json_decode() $value, then you can foreach() through it.
I managed to get a json response back from a request and i convert it to String lets say:
String response = client.execute(get, handler);
The response looks something like:
"geometry":{"rings":[[[29470.26099999994,40220.076999999583],[29551.560999999754,40324.093000000343],[29597.470999999903,40391.253000000492],[29619.849999999627,40434.842000000179],[29641.708999999799,40471.713999999687],[29701.501000000164,40574.616000000387],[29722.775999999605,40611.230000000447],[29723.673000000417,40613.234999999404]]]}
But I want to have it to look like the following one:
"Coordinates":"29470.26099999994|40220.076999999583,29551.560999999754|40324.093000000343,29597.470999999903|40391.253000000492,45360.235000003|41478.4790000003,45360.2369999997|41478.4729999993,45353.8320000004|41470.7339999992,45372.21|41468.057,45371.8090000004|41467.1390000004"
In summary i want to change the comma between two coordinates in a [ ] set to be separated by pipes"|" instead of comma and to separate a set of two coordinates with , instead of "],["
What i tried:
response = response.replace("],[","\,");
response = response.replace("[[[","\"");
response = response.replace("]]]","\"");
However it does not give me what i wanted...becuz i have no idea to achieve the replace of pipe...tot of using regex but dont know how to. can someone help me please
try something like this
String result = response.replaceAll("([^\\]])(\\,)","$1\\|").replaceAll("[\\[\\]]","");
=> ([^\\]])(\\,) => ([^\])(\,) every comma not preceded by ]
=> [\\[\\]] => [\[\]] every [ or ]
Please note that
replacing using regexp is using String.replaceAll or Pattern class
String are immutable
I think this should work:
String response = "[[[29470.26099999994,40220.076999999583],[29551.560999999754,40324.093000000343],[29597.470999999903,40391.253000000492],[29619.849999999627,40434.842000000179],[29641.708999999799,40471.713999999687],[29701.501000000164,40574.616000000387],[29722.775999999605,40611.230000000447],[29723.673000000417,40613.234999999404]]]";
response = response.replace(",", "|");
response = response.replace("]|[", ",");
response = response.replace("[[[", "\"");
response = response.replace("]]]", "\"");
System.out.println(response);
I use the following code to post :
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(VERIFY_PAYMENT_ACTIONURL);
// key is the parameter
// MERCHANT_KEY is the value
method.addParameter("form", "2");
method.addParameter("key", MERCHANT_KEY.trim());
method.addParameter("command", VERIFY_PAYMENT_COMMAND.trim());
method.addParameter("hash", hash);
method.addParameter("var1", transactionID.trim());
method.addParameter("salt", ALGORIHTM_SHA512_SALT_KEY.trim());
int statusCode = client.executeMethod(method);
if (statusCode != -1) {
in = method.getResponseBodyAsStream();
}
String text = method.getResponseBodyAsString();
System.out.println("text : "+text);
method.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
and I get the response text as follows :
a:3:{s:6:"status";i:1;s:3:"msg";s:44:"1 out of 1 Transactions Fetched Successfully";s:19:"transaction_details";a:1:{i:2298597;a:15:{s:8:"mihpayid";s:18:"403993715508098532";s:10:"request_id";N;s:12:"bank_ref_num";N;s:3:"amt";s:5:"53.77";s:4:"disc";s:4:"0.00";s:4:"mode";s:2:"CC";s:7:"PG_TYPE";s:4:"AXIS";s:7:"card_no";s:16:"512345XXXXXX2346";s:12:"name_on_card";s:3:"emu";s:4:"udf2";s:1:"0";s:7:"addedon";s:19:"2013-06-03 17:34:42";s:6:"status";s:7:"failure";s:14:"unmappedstatus";s:6:"failed";s:12:"Merchant_UTR";N;s:10:"Settled_At";N;}}}
now I want to extract the above output and put it into map like "transactionid" and other details like the following format
array('status' => '1',
'msg' => 'Transaction Fetched Successfully',
'transaction_details' =>
array(
'mihpayid' => Transaction ID,
'request_id' => Request ID,
'bank_ref_num' => Bank Reference Number,
'amt' => Amount
'disc' => Discount
'mode' => Transaction Mode (NB for Netbanking, CC for credit card, DC for Debit card, "-" for
'status' => Transaction Status
unknown)
)
);
I really do not find any common way to extract the above output. will some one help to do this?
or is it possible for me to convert the above to JSON and put them in to a map like the above?
thanks
I don't recognise it but are you sure it's not a well know format?
To turn it into JSON you'd need to
1)remove every instance of a character that is followed by a colon (and the colon)
2)replace every other semi-colons with a comma
3)replace all the other semi-colons with colons
And you'd need to do all that while taking into account that strings could contain any of those things.
From comments:
The string is in PHP serialised form. This library parses it: https://code.google.com/p/serialized-php-parser/
I'm trying to get all the associated products for a configurable product using the Magento SOAP API v2. The catalogProductLink call looks close, but doesn't handle configurable types. I don't see any other calls that contain the associated product and configurable type information for a product. How have others solved this issue?
I'm using Magento version 1.6 and the SOAP API V2 with Java.
I looked deeper into this solution and realized that you may need to override the API model (Mage_Catalog_Model_Product_Api) to achieve the results you're looking for.
In the items function (around line 90), you can do something like this:
foreach ($collection as $product) {
$childProductIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
$result[] = array(
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds(),
'website_ids' => $product->getWebsiteIds(),
'children' => $childProductIds[0],
);
}
create the folder app/code/local/Mage/Catalog/Model/Product/Link
copy the app/code/core/Mage/Catalog/Model/Product/Link/Api.php into this folder and edit it. (I’ve only done the change for the V1 but I’m pretty sure it’s as easy in V2)
replace the content of the the items() function with the following
if($type == "associated"){
$product = $this->_initProduct($productId);
try
{
$result = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
}
catch (Exception $e)
{
$this->_fault('data_invalid', Mage::helper('catalog')->__('The product is not configurable.'));
}
}
else
{
$typeId = $this->_getTypeId($type);
$product = $this->_initProduct($productId, $identifierType);
$link = $product->getLinkInstance()
->setLinkTypeId($typeId);
$collection = $this->_initCollection($link, $product);
$result = array();
foreach ($collection as $linkedProduct) {
$row = array(
'product_id' => $linkedProduct->getId(),
'type' => $linkedProduct->getTypeId(),
'set' => $linkedProduct->getAttributeSetId(),
'sku' => $linkedProduct->getSku()
);
foreach ($link->getAttributes() as $attribute) {
$row[$attribute['code']] = $linkedProduct->getData($attribute['code']);
}
$result[] = $row;
}
}
return $result;
then you can call the API like this now:
$client->call($sessionId, 'product_link.list', array('associated', $id_of_your_configurable_product));
Basically my code is checking the type provided and if it’s “associated” it returns the child products.
I’m pretty sure there’s better way of doing it but I thought that the Product Link API was the most relevant place to do it.
Enjoy!
(please note: this code is not mine, i just adapted it and thought it would be a nice idea to help you guys out)