how to parse returned value from asmx - php

I want to send some values from php to an asmx webservce.
my code work's fine, but the return value is like this :
object(stdClass)#4 (1) { ["any"]=> string(963) "123" }
now, how to get 123 value ?
require_once('lib/nusoap.php');
$client = new SoapClient("http://****/service.asmx?WSDL");
$params->UserName = '1';
$params->PassWord = '1!';
$params->Ip = '1!';
$params->MacMain = '1!';
$params->PcName = '1!';
$result = $client->GetPassPort($params)->GetPassPortResult;
var_dump($result);

while getting data from object you need to use -> operator.Just use below code to get 123 as :
$result->any

Related

json_decode() not working

I am using ajax to send data with JSON and it keeps returning null. Here is the string I'm sending:
{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}
It is sent via post. Here is the code I send it with:
function slash(strr){
var re = /([^a-zA-Z0-9])/g;
var str = strr;
var subst = '\$1';
var st = encodeURIComponent(str.replace(re,subst));
console.log("st");
return st;
}
function create() {
var info = {};
var code=editor.getValue();
info.username=username;
info.code=slash(code);
var name=document.getElementById('projectName').value;
name2=name;
info.name=slash(name2);
info=JSON.stringify(info);
console.log(info);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("demo").innerHTML = xhttp.responseText;
}
};
xhttp.open("POST", "create_project.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("info="+info);
}
When it gets received in the php file it is processed like this:
$info = $_POST['info'];
echo "<pre>".$info."</pre>";
//$info = urldecode($info);
$info = json_decode($info);
echo "<pre>".$info."</pre>";
However for some reason the json_decode() doest work. Again here is the JSON I'm sending:
{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}
the first echo works correctly but the second one doesn't. How do I fix this?
json_decode() must be emitting an error which you are not checking. Functions like json_decode() and json_encode() do not display errors, you must use json_last_error and since PHP 5.5 there is also json_last_error_msg().
<?php
$str = '{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}';
var_dump($str);
var_dump(json_decode($str));
var_dump(json_last_error());
var_dump(json_last_error_msg());
The above outputs:
string(189) "{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}"
class stdClass#1 (3) {
public $username =>
string(8) "HittmanA"
public $code =>
string(136) "%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F"
public $name =>
string(10) "Untitled-1"
}
int(0)
string(8) "No error"
If we try to decode invalid JSON:
<?php
$str = 'foobar{';
var_dump($str);
var_dump(json_decode($str));
var_dump(json_last_error());
var_dump(json_last_error_msg());
The above prints:
string(7) "foobar{"
NULL
int(4)
string(16) "boolean expected"
There must be an error in the JSON when you try to decode it. Check for errors usings the json_* error message functions. The error message will tell you what's wrong and it will be straight to fix once you know what the error is.
hello mister if you echo in php is equal to return or print in some functional programming.
if your using ajax, ajax is one way communication.
$info = $_POST['info'];
//this code will be return
echo "<pre>".$info."</pre>";
//this also will not be triggered cause you already return the above code
//$info = urldecode($info);
$info = json_decode($info);
echo "<pre>".$info."</pre>";
Perhaps setting the xhttp content-type as json like
<?PHP
$data = /** whatever you're serializing **/;
header('Content-Type: application/json');
echo json_encode($data); ?>
As stated in this answer.
Looking at the object it looks like you have a ' inside your code parameter. I think that's invalid for encoding.
Using json_decode like this:
$info = json_decode($info);
Will return a PHP variable.
If you add the associative parameter as true (false by default) like this:
$info = json_decode($info, true);
Then it will return an associative array
http://php.net/manual/en/function.json-decode.php
I was struggling with this on my Windows 10 machine, PHP version 5.5.9 only to find that the php_json.dll file was not in the ext folder of my installation and obviously no extension to uncomment in php.ini file. I found the dll at http://originaldll.com/file/php_json.dll/31989.html. After copying it to my ext folder I added extension=php_json.dll to my php.ini file and restarted my apache server and it started working fine.

Copying and adding JSON schema as array in PHP

I am trying to add a copy of a key and its content to the JSON file by using php.
$content = file_get_contents($_POST["content"]);
$decode = json_decode($content,true);
$version = $_POST["version"];
array_unshift($decode['versions'],$version );
$encode = json_encode($decode,true);
Let's say we want to add the version 1.2.2. This adds:
...["versions"]=> array(6) { [0]=> string(5) "1.2.2" ["1.2.1"]=> array(4) { ["project"]=> string(21)...
instead of creating a 1.2.2 key and its sub arrays.
Also tried:
array_unshift($decode['versions'][$version],$version );
without results either
If I undestand correctly, you can directly put the version inside your content like this :
$content = file_get_contents($_POST['content']);
$decode = json_decode($content, true);
$version = $_POST['version'];
$decode['version'] = $version;
$encode = json_encode($decode, true);

Get image url from Netsuite File Cabinet in PHP

Is it possible to grab the url Netsuite uses for an item image using the PHP Toolkit? Using this function:
function getProduct($id) {
$service = new NetSuiteService();
$service->setSearchPreferences(false, 1000);
$itemInfo = new SearchMultiSelectField();
$itemInfo->operator = "anyOf";
$itemInfo->searchValue = array('internalId' => $id);
$search = new ItemSearchBasic();
$search->internalId = $itemInfo;
$request = new SearchRequest();
$request->searchRecord = $search;
$searchResponse = $service->search($request);
$products = $searchResponse->searchResult->recordList->record;
return $products;
}
I can get the item information. In the information, netsuite gives me this little snippet (the whole thing is truncated for cleanliness):
...
[storeDisplayImage] => RecordRef Object
(
[internalId] => 19876
[externalId] =>
[type] =>
[name] => 4010024.jpg
)
...
This just tells me the name of the file in the Netsuite file cabinet. How do I download that image automatically using the PHP Toolkit?
Try this:
$request2 = new GetRequest();
$request2->baseRef = new RecordRef();
$request2->baseRef->internalId = $product->storeDisplayImage->internalId;
$request2->baseRef->type = "file";
$getResponse2 = $this->service->get($request2);
if (!$getResponse2->readResponse->status->isSuccess) {
echo "GET ERROR";
} else {
$file = $getResponse2->readResponse->record;
echo "GET SUCCESS, File name is " . $file->name;
echo "<img src='".$file->url."'>";
}
I think you will want to look at:
["storeDisplayImage"]=>
object(RecordRef)#20 (4) {
["internalId"]=>
string(6) "758653"
["externalId"]=>
NULL
["type"]=>
NULL
["name"]=>
string(26) "image_name.jpg"
}
I cannot find any way to actually access the image, but if it is possible, the internalId (758653) is what you should be using to search. It is used when accessing the image via URL:
https://system.netsuite.com/core/media/media.nl?id=758653&c=ACCOUNT#&h=31578b297cc0be65db1f&expurl=T
The problem is that random hash at the end of the URL. I can't quite figure out what it is, but if you could, you would be able to construct that URL.
EDIT: nlapiResolveURL might help you. Try passing the internalId to nlapiResolveURL and it could provide that URL dynamically.

how can call a asmx webservice by parsing an object as parameter,

Here is my code
$objLogParameter = new LogParameters();
$objLogParameter->strLogMessage = $message."&nbsp".$extendedMessage;
$objLogParameter->strStackTrace = $preStackTrace;
$objLogParameter->strUser = "Osmosys";
$objLogParameter->strCustomer = "ws";
$objLogParameter->strPageOrModuleName = "calling ws";
$objLogParameter->strApplication = "OsmTest";
$objLogParameter->strSubscription = "test2";
$objLogParameter->EnumSeverity = "Error";
$objLogParameter->EnumLogType = "ErrorTest";
$url = "http://log.cocoonit.in/writelogsindbservice.asmx?WSDL";
$client = new SoapClient($url, array("trace" => 1));
$res = $client->WriteLogInDB($objLogParameter);
WriteLogInDB is a method in .asmx webservice, thats method except an object as a argument then if i pass a object in $res i got a msg like status code = 0,object couldn't be empty.
I want to connect this Url
http://log.cocoonit.in/writelogsindbservice.asmx
and in this it have a WriteLogInDB method, i want to call this method by passing an object nothing but $objLogParameter. how can i do it in php.
Please help me.

Cannot instantiate a Note object using Evernote API in PHP

I am trying to develop a script (using the PHP example app as a basis) that will post a note to Evernote based on GET values.
When I insert this to the end of functions.php's listNotebooks()
$note_obj = new Note(array(
'contents' => $note_contents,
'title' => $title
));
It throws a 500 error. (In my code, $title & $note_contents are defined earlier. I have spent a lot of time trying to find proper documentation for the PHP API but it just seems non-existent. Any information on this topic would be greatly appreciated
Update: I did not realize the API was using PHP Namespaces: This fixed the issue:
//import Note class
use EDAM\Types\Note;
use EDAM\Types\NoteAttributes;
use EDAM\NoteStore\NoteStoreClient;
My code to add a note still does not work but I'll post it here once I figure it out.
These classes need to be imported:
//import Note class
use EDAM\Types\Note;
use EDAM\Types\NoteAttributes;
use EDAM\NoteStore\NoteStoreClient;
This will define our new note:
$noteAttr = new NoteAttributes();
$noteAttr->sourceURL = "http://www.example.com";
$note = new Note();
$note->guid = null;
$note->title = "My Title";
$note->content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml.dtd"><en-note>My Content</en-note>';
$note->contentHash = null;
$note->contentLength = null;
$note->created = time()*1000;
$note->updated = time()*1000;
$note->deleted = null;
$note->active = null;
$note->updateSequenceNum = null;
$note->notebookGuid = null;
$note->tagGuids = null;
$note->resources = null;
$note->attributes = $noteAttr;
$note->tagNames = null;
addNote($note);
This function will add a new note:
function addNote($newnote) {
global $noteRet;
if (empty($noteRet)) {
define("noteStoreHost", "sandbox.evernote.com");
define("noteStorePort", "80");
define("noteStoreProto", "https");
define("noteStoreUrl", "edam/note/");
$noteStoreTrans = new THttpClient(noteStoreHost, noteStorePort, noteStoreUrl . $_SESSION['shardId'], noteStoreProto);
$noteStoreProt = new TBinaryProtocol($noteStoreTrans);
$noteStore = new NoteStoreClient($noteStoreProt, $noteStoreProt);
$noteRet = $noteStore->createNote($_SESSION['accessToken'], $newnote);
return $noteRet;
}
}

Categories