How to use Post to URL feature of plugin - php

I'm wondering if anyone could help me out in trying to use a feature of my form plugin (FormCraft for Wordpress) that allows the form data to be sent to a URL. There's no documentation on this feature and I can't figure it out. Here's a screenshot of the options:
I've created an empty form_data.php file and uploaded it to my site, then put that URL in the field seen in the screenshot. Nothing happens in the php file when I submit a form. The support team for the plugin hasn't been very helpful, they just said, "You need to make sure the script on that URL will catch and parse the data." Is there anything that I would need to add to the php file to get this to work?

Send the data to this file below.
http://localhost/bellow_file.php
<?php
$json = json_encode($_REQUEST, JSON_UNESCAPED_UNICODE);
$json = json_decode($json,true);
$data = "";
foreach ($json as $key => $val) {
if(is_array($val)) {
$data .= "$key:\n";
} else {
$data .= "$key => $val\n";
}
}
file_put_contents('test.txt', $data);
?>

Related

cannot print values - using PHP

I am trying to print date values before form submission but it did not print anything and submitted the form, below is my code
function webforms_save_submission_alter(&$fields, $context)
{
foreach ($fields as $key => $value) {
if (in_array($key, ["Date_Prototype__c", "Date_Production__c", "NBIOT_Date_of_Field_Trial__c", "NBIOT_Date_Lab_Trial__c"])){
$fields[$key] = trim($value);
print_r($fields); // printing here
if ($fields[$key] == 'Date_Production__c') {
$fields[$key] = format_date(strtotime($fields['Date_Production__c']), 'medium', 'm-d-Y');
print_r($fields); // printing here
die;
}
I want to see the date reactions before submitting the form but why it did not print anythign? can someone please help me on this?
Your help will be highly appreciated..
There are a number of problems with that code, which I suspect you've only written to debug a problem? It's much too complicated for that. Also, die is a function, and needs to be called with parentheses; Syntax errors are also a likely reason for why your code did nothing. Check your watchdog logs.
Try this code instead:
function webforms_save_submission_alter(&$fields, $context)
{
var_dump($fields);
die();
}

Modifying json with PHP not saving/updating

I am trying to modify data in a json file with php. The code I am using is below. It is able to successfully ready the file contents and in the foreach loop it will echo out in the if statement.
This is great, the if statement is hardcoded for now to test. What I want to do is modify various properties and write it back to the file. This does not seem to be working. When I load the page, then refresh to see if the new value was set it just keeps echoing the same values. I download the .json locally and nothing has changed.
Any thoughts on what I am doing wrong?
//Get file, decode
$filename = '../json/hartford.json';
$jsonString = file_get_contents($filename);
$data = json_decode($jsonString, true);
foreach ($data['features'] as $key => $segment) {
if ($segment['properties']['UID'] == '25301') {
//echo out properties for testing
echo("KEY: ".$key."<br/>");
echo("UID: ".$segment['properties']['UID']."<br/>");
echo("Full Name: ".$segment['properties']['FULLNAME']."<br/>");
echo("FCC: ".$segment['properties']['FCC']."<br/>");
echo("Render CL: ".$segment['properties']['RENDER_CL']."<br/>");
echo("<hr/>");
//set property to new value.... NOT WORKING?
$segment['properties']['RENDER_CL'] = 111;
}
}
//Test if file is writable to be sure
$writable = ( is_writable($filename) ) ? TRUE : chmod($filename, 0755);
if ( $writable ) {
$newJsonString = json_encode($data);
if (file_put_contents($filename, $newJsonString)) {
echo('Put File Content success');
} else {
echo('NOT put');
}
} else {
echo 'not writeable';
}
In the end it will echo out 'Put File Content success' which seems to indicate it was successful but it isn't... Thanks for any advice.
You need to understand how foreach cycle works. The thing is, that the value you're getting ($segment) is a copy of the real value in the source array. So when you assign to it ($segment['properties']['RENDER_CL'] = 111;), you don't really change the source array. You only change some local variable that goes out of scope when the cycel-loop ends.
There are several ways how to solve this issue. One of them is to add & before the value-variable:
foreach ($data['features'] as $key => &$segment)
This tells it to use the reference of the array-item, not to copy its value.
You should use $segment variable in foreach as a reference:
foreach ($data['features'] as $key => &$segment) {
...
$segment['properties']['RENDER_CL'] = 111;
}

Creating a Custom PHP bot

I am trying to create a bot to automatically scrape some data from a website. I am trying to use the Snoopy PHP class for this.
The problem I am having is that I cannot submit the login form page. I am passing the username and password in the field, but "nothing happens" - I don't get logged in and an error is not returned (such as invalid password).
I have tried using cURL directly (without the Snoopy class), but that doesn't help.
I feel that I am not passing the form variables correctly. I would appreciate it if someone could point me in the right direction.
The code I am using is:
$snoopy = new Snoopy;
$submit_url = "";
$submit_vars['ctl00$cphMainContent$ctl00$UsernameTextBox'] = "";
$submit_vars['ctl00$cphMainContent$ctl00$PasswordTextBox'] = "";
if($snoopy->submit($submit_url,$submit_vars)) {
while(list($key,$val) = each($snoopy->headers)) {
echo $key.": ".$val."<br>\n";
}
echo "<p>\n";
echo "<PRE>".htmlspecialchars($snoopy->results)."</PRE>\n";
} else {
echo "error fetching document: ".$snoopy->error."\n";
}

JSON PHP parsing search API

Alright, I'm using the Blekko search API:
http://blekko.com/ws/?q=hello+%2Fjson
how would I go about parsing it ?
I have no experience of parsing JSON from PHP, so I'd appreciate a little help, and the json_decode() docs failed to explain everything for me, particularly getting the data inside RESULT. :) You know, [ and ].
Could you help pointing me in the right direction ? :)
Thank you, you're all so helpful! :)
Here's the code to access the API.
You should enter your own error/unexpected results handling where i've left the comments.
$data = file_get_contents('http://blekko.com/ws/?q=hello+%2Fjson');
if(!empty($data)){
$data = json_decode($data);
if(!empty($data->ERROR)){
// Error with API response.
} else {
$data = $data->RESULT;
if(empty($data)){
// No results.
} else {
// Uncomment the line below to see your data
// echo '<pre>' . print_r($data) . '</pre>';
foreach($data AS $key => $val){
echo $val->short_host . '<br />';
}
}
}
} else {
// Failed to retrieve data.
}

Webkit cutting off last character of a json feed

I'm creating a JSON feed with PHP using echo json_encode($my_array).
The resulting string passed to the browser should be as shown below:
[{"ACC_NUM":"147545","BOOK_DATE":"2011-10-22"},{"ACC_NUM":"147546","BOOK_DATE":"2011-10-22"}]
In Firefox, the json string is received as shown. However, in webkit browsers (Safari, Chrome), the last character of the string gets cut off. This results in a parse error.
I've even tried serving the feed as application/json and text/html to no avail.
Does anyone know what's going on? Am I doing something wrong here? Thanks in advance.
BACKEND CODE:
// $result contains rows from a mysql query
if($result) {
$arr = array();
foreach($result as $key => $val) {
$arr[$key] = $val;
}
echo json_encode($arr);
}
this is saved in index.php which I can view in the browser and shows the resulting json feed.
For those who encounter this problem with Slimframework.
After echoing the json string, just add an exit command.
if($result) {
$arr = array();
foreach($result as $key => $val) {
$arr[$key] = $val;
}
echo json_encode($arr);
exit; // <------------------- Hallelujah!!!
}

Categories