I am (trying) to build a web app, first one :)
I need to call a JSON object though, and I am using a simple search form to get a word that I will append to the call, but I need PHP to handle the object when it is returned. How can I do this?
Basically I had the form submit calling a function that, ideally, would go get the object and do what it needs to, then return the results formatted how I want. Does that make sense even?
Thank you!
PHP 5.x has some built in functions: json_decode() and json_encode(). They are worth looking into and should answer your question. If you have less then PHP 5.x the user comments at both of those pages should have alternatives to use in it's place.
Related
So I'm working with the wp rest api and the wp rest controller plugin. I have a music theme that's doing some interesting things. When i make the API call one of the properties i get is this:
"subheader_img": [
"a:1:{i:0;a:5:{s:5:\"image\";s:0:\"\";s:5:\"color\";s:0:\"\";s:6:\"repeat\";s:6:\"repeat\";s:8:\"position\";s:8:\"left top\";s:11:\"attachement\";s:6:\"scroll\";}}"
],
its a string, that i know should be an array. I'm not sure what type encoding is applied to it. But i'm trying to clean it up on the javascript side. Does anyone know what's being done to this array turned string? And if there are any javascript functions that can help? I've tried JSON.Parse and that obviously won't work because... its not JSON. I've also tried stripping away the first few characters string.substring(x), but i run into the same problem after that.
i know its something simple, i'm just not sure what.
If its possible I'd like to parse it in javascript, modifying the endpoint might be more difficult because of the wordpress theme.
Use WP's functions for serialization:
is_serialized
is_serialized_string
maybe_serialize
maybe_unserialize
The gist: Check if your array item is a serialized string (is_serialized_string), and then maybe_unserialize it. Then you can evaluate the result to see if it's in a format you're expecting (such as an array).
I am very new to programming and need a little help with getting data from a website and passing it into my PHP script.
The website is http://www.birthdatabase.com/.
I would like to plug in a name (First and Last) and retrieve the result. I know you can query the site by passing the name in the URL, but I am having problems scraping the results.
http://www.birthdatabase.com/cgi-bin/query.pl?textfield=FIRST&textfield2=LAST&age=&affid=
I am using the file_get_contents($URL) function to get the page but need help after that. Specifically, I would like to scrape only the results from a certain state if there are multiple results for that name.
Thanks for your help.
Read about POST method and its array ($_POST) ---> reference.
Similarly, there is GET method as well.
Why dont you see POST AND GET method?
I've used Google, Yahoo, AND Bing, but I can't find any good answers. I've seen jLinq, but I want to be able to query JSON in PHP in hopes of having a not an SQL database, but instead all data storage within the filesystem on my server. No, I don't care how bad it sounds.
Ideas nonetheless? I would think that there would be a PHP class on this.
-----EDIT-----
Guys, thanks for your answers so far, but I don't think that json_encode and json_decode are of much use. What I want to be able to do is encode/decode JSON, and be able to search it for specific keys with specific values. Albeit I have PROVEN to myself that I can do so, it's a lot of code for something that should be so simple. Anything else you have in mind?
You can use JsonQ package. This package can query over JSON data like Query Builder.
https://github.com/nahid/jsonq
You can call json_encode to convert your data to a string, and write this string to a file. Then when you want to use it, you read the entire file and call json_decode to convert it back to data. When you're done processing it, repeat the encode/write steps.
But if you have multiple processes doing this, they'll completely overwrite what each other is doing. So it's not a very good way to manage shared data.
You could try JSONPath it allows you to query json with xpath as you would xml.
Look into json_decode. This lets you take JSON as a string and parse it into an object in PHP. You could theoretically store the strings as files, and use file_get_contents to retrieve the string from the file.
You would have to write your own searching/indexing/updating/etc algorithms, but if you don't want to use a real database solution (since you said, No, I don't care how bad it sounds.), then I guess this would work.
To search for values in the object you get from json_decode, look into in_array and array_search
Ok, I usually do this in reverse, post a specific data set, then expect an JSON object back, however. In today's case I kinda need to the opposite. Except my confusion is brought on by there is no static elements to work with. My data won't be coming from a customary form, so serialization is not an option I don't think. So before I go more into it a little info on the spec
I have a Unordered list that can have anywhere from 1 List Element to dozens, each one contains 3 pieces if info I need to pass in a POST to my PHP as I would a form. But Im not sure whats the best way to handle it.
Can I pass it as an array through the post where the array is what it is? Or do I have to transform the output to resemble a JSON object and post that, treating it as a JSON object with the PHP and running it through json_decode? Not sure what to do here, so any advice is greatly appreciated.
edit worth mentioning is I can get the data I need from the list elements, its just what should I do with it to pass it in a post, so that it posts in sense like its a multidimensional object or array
You can pass the array from js using ajax as array. And it will come to $_POST as array without any additional steps
You need to transform it into a JSON string and then json_decode it on the other side. This is where node.js can be superior as a backend because you can pass the array directly to Node without going into and out of JSON.
I'm trying to parse information from fonefinder.net. I was trying to use simplexmlload_file, but couldn't get the page to load successfully.
Now, I'm looking into Curl. But I'm not sure if this will work either.
I basically just want to take the html from the fonefinder page, and parse it to get phone carrier and city.
Is that possible? How?
SimpleXML will only work if the HTML is formatted correctly - and that is rarely the case ;)
You could do a simple cURL call to fetch the data and the easiest thing would probably be using a regular expression to get the information you need.
The solution however is not easy to supply you with, with nothing to go on. But this was an idea.
I Recommend using:
http://www.de.php.net/manual/en/function.file-get-contents.php to get the document
http://www.php.net/manual/en/domdocument.loadhtml.php to load it
http://www.php.net/manual/en/class.domxpath.php to get the information from it
Or use the search function here, that question must have been asked over and over, for example PHP: Fetch content from a html page using xpath()