This question already has answers here:
Parsing JSON with PHP
(6 answers)
Closed 7 years ago.
I know this one has been asked loads of times, so mods please do not close this off as no past answers have worked and I have searched already! So I'm using the HaveIBeenPwned API which outputs what is apparently JSON like so:
[
{
"Title":"000webhost"
,"Name":"000webhost"
,"Domain":"000webhost.com"
,"BreachDate":"2015-03-01"
,"AddedDate":"2015-10-26T23:35:45Z"
,"PwnCount":12345678
,"Description":"In approximately March 2015, the free web hosting provider 000webhost suffered a major data breach that exposed over 13 million customer records. The data was sold and traded before 000webhost was alerted in October. The breach included names, email addresses and plain text passwords."
,"DataClasses":[
"Email addresses"
,"IP addresses"
,"Names"
,"Passwords"
]
,"IsVerified":true
,"IsSensitive":false
,"LogoType":"png"
}
]
That is the output if I use the following:
echo $output;
json_decode($output) doesn't work. Using a foreach($output as $key) doesn't work, it says invalid argument if I use that. I have tried a normal for() loop and again no joy. What is wrong with this output? How do I get at the values within it? As you should be able to tell I'm using PHP.
Use json_decode($output, true);
Second parameter defines result data format - object (false, default) or associated array (true).
Related
This question already has answers here:
StaleElementReference Exception in PageFactory
(3 answers)
Closed 3 years ago.
Good afternoon,
I am trying to open several links within a foreach using Facebook WebDriver, but it only opens the first one.
The links are arriving correctly, string, but on the second link it hangs and displays the error below.
It seems to me that selenium drive has a cookie or cache problem.
foreach($subjectsList as $subject)
{
// Get all info subjects each course.
$subjectTitle = $subject->getText();
$subjectLink = trim($subject->getAttribute('href'));
$rawPage = $this->seleniumDriver->get($subjectLink);
} // end Foreach for subjectsLis
Thanks in advance guys, any help is very welcome.
It's not a cookie or a cache problem, it's conceptual.
$rawPage = $this->seleniumDriver->get($subjectLink); performs browser navigation. So, your code will correctly parse out the first link, and navigate to it. But, when it tries the second link, selenium correctly identifies that $subject is essentially a dangling pointer (well, it contains a C pointer which is technically the dangling pointer, but...) to a DOM element in the previous (deallocated) webpage/dom-tree.
To do what you're looking for, first parse out the titles/hrefs to strings, then iterate over the strings.
I'd take a look at the relevant documentation
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I'm trying to fetch data from a MariaDB database of festivals and respective locations. When running:
while($row = $sth->fetch_assoc()){}
and iterating over $row while outputting the values, I get data as it is in the database.
However, when storing each row like so:
while($row = $sth->fetch_assoc()){
$results[] = $row;
}
And echoing the results as JSON (echo json_encode($results);)
I get this:
{"id":"0","name":null,"village":"0","startDate":"2019-01-16",
"endDate":"2019-01-23","message":null}
This is for an existing Linux server, which I do not manage (I'm using CPanel). PHP version is 5.4 and MariaDB 10.1.37.
So far, a lot of code samples on Stack Overflow and other websites are using
$results[] = $row;
for storing the results.
I'm returning to PHP after 3 years of Swift only programming... So I suspect this could be a simple issue to solve...
Thanks!
May be I'm bit too late for answering this question, but it may help someone stuck with this issue. Recently I faced a similar issue and it took me 3 days to understand that it is an issue with the utf-8 characters. Data stored in database are perfectly fine. But when it comes to return it via json_encode(), it shows no data at all as json_encode only supports utf-8 data(reference).
For your case the following method should work-
foreach ($results as &$r) {
$r['name'] = utf8_encode($r['name']);
//same for all other items
}
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Receive JSON POST with PHP
(12 answers)
Closed 4 years ago.
I'm currently trying to implement a system that posts data as 'multipart/form-data' and I am receiving the data through php://input as follows:
[
{
"id":"595944535043",
"companyProfileID":"",
"accountID":"",
"ownerID":"",
"importID":"",
"avatarUrl":"https://d2ojpxxtu63wzl.cloudfront.net/static/a9b1a30bbf8148445118d0d70ebd4a01_16edde90d640c6ee84a1874a4dbb3cdc816bc9e94ec6a23efc885e59947b28ab",
"companyName":"",
"title":"",
"firstName":"Madison",
"lastName":"Test",
"street":"",
"city":"",
"country":"",
"state":"",
"zipcode":"",
"emailAddress":"email#email.com",
"website":"",
"phoneNumber":"",
"officePhoneNumber":"",
"phoneNumberExtension":"",
"mobilePhoneNumber":"",
"faxNumber":"",
"description":"",
"campaignID":"",
"crmLeadID":"",
"crmOpportunityID":"",
"crmAccountID":"",
"crmContactID":"",
"crmOwnerID":"",
"crmThirdPartyID":"",
"formGUID":"",
"trackingID":"",
"leadSource":"",
"industry":"",
"active":"1",
"isQualified":"",
"isContact":"1",
"isCustomer":"0",
"hasOpportunity":"0",
"lastActivityDate":"2018-06-08 15:04:10",
"updateTimestamp":"2018-06-17 09:25:39",
"updateUserProfileID":"0",
"createTimestamp":"2018-06-08 14:04:10",
"createUserProfileID":"313407940",
"leadStatus":"contact",
"persona":"",
"services_5addf517bd49b":""
}
]
However when I try and access any of these details via $_POST it dosen't return anything. A quick check indicates that $_POST is in fact empty.
I've exhausted all options on this question
php $_POST array empty upon form submission
And this one PHP some $_POST values missing but are present in php://input
Changed my max_input_vars to 10000, post_max_size is 1G. Tried JSON decode solutions, parsestr solutions, nothing.
So just wondering if there's anybody out there who can help?
Not a duplicated question as the data is nested. Different to previous problems on Stack overflow
Wow so I managed to figure it out.
I took the POST data that i got via php://input and created a JSON variable with the data. Then after some testing I tried to see if treating it like a nested array after performing the $posted_data = json_decode(file_get_contents("php://input"));.
So i called the data using the following:
$posted_data[0]->emailAddress
And that worked... hope this can help anyone in the future and save the amount of hours i've just wasted :)
Increase the postmaxsize variable in php.ini was set to higher than the amount of physical RAM in the machine also use M suffix instead MB
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
Have tried every variation I can think of to access an object property in an array. I'm getting some data back form an API which i'm storing in a variable called $userTokenValid:
$userTokenValid = [{"authTokenValid":1}];
i'm then trying to access the authTokenValid property like so:
echo json_decode($userTokenValid[0]->authTokenValid);
I appreciate this might be quite basic but can't spot where I have gone wrong.
$userTokenValid isn't valid php. However [{"authTokenValid":1}] is a valid json string.
$userTokenValid = '[{"authTokenValid":1}]';
you can decode it with
$json = json_decode($userTokenValid);
finally
echo $json[0]->authTokenValid;
This question already has answers here:
get json using php
(3 answers)
Closed 9 years ago.
When you're visiting this page:
https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns
...I guess what is called an object, appears with a lot of information. But when I try to do a simple GET call and then print it, like so:
$tweets = $_GET['https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns'];
print_r($tweets);
...It returns nothing... Why is that?
The $_GET super global is populated with the query string of the current request, it's not used to get stuff from the interwebs.
You're looking for file_get_contents():
$url = 'https://www.googleapis.com/plus/v1/activities/z12gtjhq3qn2xxl2o224exwiqruvtda0i?key=AIzaSyAwnz3yjIcvsosfbudkzl9oogGrT21m6Ns';
$tweets = file_get_contents($url);
print_r($tweets);
If that contains a JSON encoded response you need to additionally use json_decode() to use it.
Do you know how to use $_GET?
Check PHP documentation, you must have made a mistake, or you're using it the wrong way.