fetch JSON data through php code - javascript

Method Name: GetAvaiillabiilliity
$mySOAP = <<<EOD
{
"Authentication": {
"LoginId": "username",
"Password": "password"
},
"AvailabilityInput":{
"BookingType":"O",
"JourneyDetails":[{
"Origin":"BOM",
"Destination":"PNQ",
"TravelDate":"10/30/2016"
}],
"ClassType":"Economy",
"AirlineCode":"",
"AdultCount":1,
"ChildCount":0,
"InfantCount":0,
"ResidentofIndia":1,
"Optional1":"0",
"Optional2":"0",
"Optional3":"0"
}
}
EOD;
// The HTTP headers for the request (based on image above)
$headers = array(
'Content-Type: application/json',
);
// Build the cURL session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $mySOAP);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// Send the request and check the response
if (($result = curl_exec($ch)) === FALSE) {
die('cURL error: ' . curl_error($ch) . "<br />\n");
} else {
echo $result;
}
curl_close($ch);
$data = json_decode($result,true);
$devices = $data['AvailabilityOutput']['AvailableFlights'];
?>
Output: GetAvailabilityResponse
This is the response which we have got from the server side and I want to show this through my php and html code.
{
"ResponseStatus":1,"UserTrackId":"RMYLN97099869978897983977484056886242191","AvailabilityOutput":{"AvailableFlights":{"OngoingFlights":[{"AvailSegments":[{"FlightId":"41","AirlineCode":"9W","FlightNumber":"618","AirCraftType":"738","Origin":"BOM","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 10:00:00","ArrivalDateTime":"30\/10\/2016 10:50:00","Duration":"00Hrs 50Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":6940,"YQ":200,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":8254,"Commission":"208.2"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"42","AirlineCode":"9W","FlightNumber":"453","AirCraftType":"738","Origin":"BOM","OriginAirportTerminal":"","Destination":"HYD","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 03:00:00","ArrivalDateTime":"30\/10\/2016 04:15:00","Duration":"01Hrs 15Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"L","ClassCodeDesc":"L","BaggageAllowed":null,"Adult":{"FareBasis":"L2IPO","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"43","AirlineCode":"9W","FlightNumber":"2822","AirCraftType":"73W","Origin":"HYD","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 05:50:00","ArrivalDateTime":"30\/10\/2016 07:05:00","Duration":"01Hrs 15Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"L","ClassCodeDesc":"L","BaggageAllowed":null,"Adult":{"FareBasis":"L2IPO","FareType":"\u000aRefundable","BasicAmount":16540,"YQ":400,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":18965,"Commission":"496.2"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"44","AirlineCode":"9W","FlightNumber":"465","AirCraftType":"738","Origin":"BOM","OriginAirportTerminal":"","Destination":"MAA","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 07:05:00","ArrivalDateTime":"30\/10\/2016 09:05:00","Duration":"02Hrs 0Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"45","AirlineCode":"9W","FlightNumber":"2491","AirCraftType":"738","Origin":"MAA","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 09:45:00","ArrivalDateTime":"30\/10\/2016 11:25:00","Duration":"01Hrs 40Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":14050,"YQ":600,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":16244,"Commission":"421.5"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"46","AirlineCode":"9W","FlightNumber":"339","AirCraftType":"73W","Origin":"BOM","OriginAirportTerminal":"","Destination":"DEL","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 03:00:00","ArrivalDateTime":"30\/10\/2016 05:05:00","Duration":"02Hrs 5Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"47","AirlineCode":"9W","FlightNumber":"365","AirCraftType":"73H","Origin":"DEL","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 08:30:00","ArrivalDateTime":"30\/10\/2016 10:40:00","Duration":"02Hrs 10Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":13600,"YQ":800,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":16031,"Commission":"408"},"Child":null,"Infant":null}],"SupplierId":"1"}]},{"AvailSegments":[{"FlightId":"48","AirlineCode":"9W","FlightNumber":"463","AirCraftType":"73H","Origin":"BOM","OriginAirportTerminal":"","Destination":"MAA","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 02:55:00","ArrivalDateTime":"30\/10\/2016 04:50:00","Duration":"01Hrs 55Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPOA","FareType":"\u000aRefundable","BasicAmount":0,"YQ":0,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":0,"Commission":"0"},"Child":null,"Infant":null}],"SupplierId":"1"},{"FlightId":"49","AirlineCode":"9W","FlightNumber":"2491","AirCraftType":"738","Origin":"MAA","OriginAirportTerminal":"","Destination":"PNQ","DestinationAirportTerminal":"","DepartureDateTime":"30\/10\/2016 09:45:00","ArrivalDateTime":"30\/10\/2016 11:25:00","Duration":"01Hrs 40Mins","NumberofStops":0,"Via":" ","CurrencyCode":"INR","Currency_Conversion_Rate":"","AvailPaxFareDetails":[{"ClassCode":"S","ClassCodeDesc":"S","BaggageAllowed":null,"Adult":{"FareBasis":"S2IPO","FareType":"\u000aRefundable","BasicAmount":13800,"YQ":600,"TaxDetails":null,"TotalTaxAmount":0,"GrossAmount":15979,"Commission":"414"},"Child":null,"Infant":null}],"SupplierId":"1"}]}],"ReturnFlights":null}}}
my php code
<?php
echo 'Flifht checking';
echo '<br><hr>';
foreach ($devices as $device){
echo '<h4>';
echo $device['FlightNumber'];
echo '</h4>';
}
?>
How to get all flight information in json code ?
So how to get this json data AvailabilityOutput":{"AvailableFlights":{"OngoingFlights":[{"AvailSegments". I can't understand depth tree structure.

Here you have a possible implementation to go through all the flights.
I changed $devices name to $availableFlights to make the example easier to understand.
$data = json_decode($result, true);
$availableFlights = $data['AvailabilityOutput']['AvailableFlights'];
echo 'Flifht checking';
echo '<br><hr>';
foreach ($availableFlights['OngoingFlights'] as $availableFlight) {
foreach ($availableFlight['AvailSegments'] as $availSegment) {
echo '<h4>';
echo 'Flight Number: ' . $availSegment['FlightNumber'];
echo '</h4>';
echo '<p>Origin: ' . $availSegment['Origin'] . ' - Destination: ' . $availSegment['Destination'] . '</p>';
}
}
The output here is:
Flifht checkingFlight Number: 618Origin: BOM - Destination: PNQFlight Number: 453Origin: BOM - Destination: HYDFlight Number: 2822Origin: HYD - Destination: PNQFlight Number: 465Origin: BOM - Destination: MAAFlight Number: 2491Origin: MAA - Destination: PNQFlight Number: 339Origin: BOM - Destination: DELFlight Number: 365Origin: DEL - Destination: PNQFlight Number: 463Origin: BOM - Destination: MAAFlight Number: 2491Origin: MAA - Destination: PNQ
Also, a little trick: if you want to see the whole json tree easily, you can use this:
echo "<pre>";
print_r($availableFlights);
echo "</pre>";
(I thought it could be helpful to understand the data structure)

Related

Uncaught ReferenceError: $result is not defined

The below script means for cross domain and pull out the JSON result for the select menu. Currently the CURL able to echo out the $result but unable to display in the select menu with console log error: Uncaught ReferenceError: the $result is not defined. I need help on diagnosing this matter.
<?php
$data_string = json_encode($data, JSON_UNESCAPED_SLASHES);
$ch = curl_init('https://xxxxxxx.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
$result = curl_exec($ch);
echo ($result);
?>
<script>
let dropdown = $('#title-dropdown');
dropdown.empty();
dropdown.append('<option selected="true" disabled>Choose Title</option>');
dropdown.prop('selectedIndex', 0);
const url = $result;
$.getJSON(url, function (data) {
$.each(data, function (key, entry) {
dropdown.append($('<option></option>').attr('value', entry.description).text(entry.display_name));
})
});
</script>
You aren't outputting the PHP $result variable to the Javascript, you're telling javascript to look for a variable that doesn't exist. Try
const url = '<?php echo $result ?>';
it's been a while since I've done any PHP but that should at least put you on the right track if it isn't 100% correct.
Change this
const url = $result;
$.getJSON(url, function (data) {
$.each(data, function (key, entry) {
dropdown.append($('<option></option>').attr('value', entry.description).text(entry.display_name));
})
});
to this:
const json = $.parseJSON('<?php echo $result; ?>');
var result = json.result;
for (var i = 0; i < result.length; i++) {
for (var key in result[i]) {
console.log('KEY: ' + key + 'VALUE: ' + result[i][key]);
}
}

XmlHttpRequest - post JSON - json string too many quotes (i think) [duplicate]

I looked around a lot before posting this question so my apologies if it is on another post and this is only my second quesiton on here so apologies if I don't format this question correctly.
I have a really simple web service that I have created that needs to take post values and return a JSON encoded array. That all worked fine until I was told I would need to post the form data with a content-type of application/json. Since then I cannot return any values from the web service and it is definitely something to do with how I am filtering their post values.
Basically in my local setup I have created a test page that does the following -
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/'); // Set the url path we want to call
$result = curl_exec($curl);
//see the results
$json=json_decode($result,true);
curl_close($curl);
print_r($json);
On the webservice I have this (I have stripped out some of the functions) -
<?php
header('Content-type: application/json');
/* connect to the db */
$link = mysql_connect('localhost','root','root') or die('Cannot connect to the DB');
mysql_select_db('webservice',$link) or die('Cannot select the DB');
if(isset($_POST['action']) && $_POST['action'] == 'login') {
$statusCode = array('statusCode'=>1, 'statusDescription'=>'Login Process - Fail');
$posts[] = array('status'=>$statusCode);
header('Content-type: application/json');
echo json_encode($posts);
/* disconnect from the db */
}
#mysql_close($link);
?>
Basically I know that it is due to the $_POST values not being set but I can't find what I need to put instead of the $_POST. I tried
json_decode($_POST), file_get_contents("php://input") and a number of other ways but I was shooting in the dark a bit.
Any help would be greatly appreciated.
Thanks, Steve
Thanks Michael for the help, that was a definite step forward I now have at least got a repsonse when I echo the post....even if it is null
updated CURL -
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
updated php on the page that the data is posted to -
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON, TRUE ); //convert JSON into array
print_r(json_encode($input));
As I say at least I see a response now wheras prior it was returning a blank page
You have empty $_POST. If your web-server wants see data in json-format you need to read the raw input and then parse it with JSON decode.
You need something like that:
$json = file_get_contents('php://input');
$obj = json_decode($json);
Also you have wrong code for testing JSON-communication...
CURLOPT_POSTFIELDS tells curl to encode your parameters as application/x-www-form-urlencoded. You need JSON-string here.
UPDATE
Your php code for test page should be like that:
$data_string = json_encode($data);
$ch = curl_init('http://webservice.local/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result = json_decode($result);
var_dump($result);
Also on your web-service page you should remove one of the lines header('Content-type: application/json');. It must be called only once.
Hello this is a snippet from an old project of mine that uses curl to get ip information from some free ip databases services which reply in json format. I think it might help you.
$ip_srv = array("http://freegeoip.net/json/$this->ip","http://smart-ip.net/geoip-json/$this->ip");
getUserLocation($ip_srv);
Function:
function getUserLocation($services) {
$ctx = stream_context_create(array('http' => array('timeout' => 15))); // 15 seconds timeout
for ($i = 0; $i < count($services); $i++) {
// Configuring curl options
$options = array (
CURLOPT_RETURNTRANSFER => true, // return web page
//CURLOPT_HEADER => false, // don't return headers
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 5, // timeout on connect
CURLOPT_TIMEOUT => 5, // timeout on response
CURLOPT_MAXREDIRS => 10 // stop after 10 redirects
);
// Initializing curl
$ch = curl_init($services[$i]);
curl_setopt_array ( $ch, $options );
$content = curl_exec ( $ch );
$err = curl_errno ( $ch );
$errmsg = curl_error ( $ch );
$header = curl_getinfo ( $ch );
$httpCode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
curl_close ( $ch );
//echo 'service: ' . $services[$i] . '</br>';
//echo 'err: '.$err.'</br>';
//echo 'errmsg: '.$errmsg.'</br>';
//echo 'httpCode: '.$httpCode.'</br>';
//print_r($header);
//print_r(json_decode($content, true));
if ($err == 0 && $httpCode == 200 && $header['download_content_length'] > 0) {
return json_decode($content, true);
}
}
}
you can put your json in a parameter and send it instead of put only your json in header:
$post_string= 'json_param=' . json_encode($data);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl, CURLOPT_URL, 'http://webservice.local/'); // Set the url path we want to call
//execute post
$result = curl_exec($curl);
//see the results
$json=json_decode($result,true);
curl_close($curl);
print_r($json);
on the service side you can get your json string as a parameter:
$json_string = $_POST['json_param'];
$obj = json_decode($json_string);
then you can use your converted data as object.

For each php loop get results added via AJAX

I have a working AJAX request call, that calls to a PHP file(which has some looping) and depending on some parameters in can take several minutes for the request to be ready. But waiting several minutes is not really user friendly.
How should i modify my code in order to have results outputed in HTML after after each for loop is over?
I understand this streaming effect could be accomplished with API's like web-sockets or socket.io, but I hope I can manage to accomplish this without needing to implement the use of these API's .
Live example witch the effect i am going for:
http://www.brokenlinkcheck.com/
I have made a demo of my code with the core of my logic in it:
PHP File :
<?php
$html = file_get_html($url);
function check_url($a)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $a);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
return $headers['http_code'];
}
$good_array = array();
$number = 1;
foreach ($html->find('a') as $element) { // this one should return results first
$a = $element->href;
$check_url_status = check_url($a);
if (!preg_match('/404/', $check_url_status)) {
echo "<p style='color:green'>" . $number . '. ' . $a . " - Works (Response: $check_url_status) </p>";
$good_array[] = $a;
} else {
echo "<p style='color:red'>" . $number . '. ' . $a . " - Broken (Response : $check_url_status) </p>";
}
$number++;
}
array_unique($good_array);
for ($x = 0; count($good_array) > $x; $x++) { // from then on for every ending of this loop - it should add new html output via ajax.
$html = file_get_html($good_array[$x]);
foreach ($html->find('a') as $element) {
$a = $element->href;
$check_url_status = check_url($a);
if (!preg_match('/404/', $check_url_status)) {
echo "<p>" . $number . '. ' . $a . " - Works (Response : $check_url_status) | src: $good_array[$x] </p>";
} else {
echo "<p>" . $number . '. ' . $a . " - Broken (Response : $check_url_status) | src: $good_array[$x] </p>";
}
$number++;
}
}
?>
jQuery AJAX:
$(document).ready(function () {
$("#ajax-btn").click(function () {
$.ajax({
url: "../broken_links",
type: "get",
success: function (result) {
$("#div1").load('http://website.dev/php');
// alert('works');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
});
(Would be perfect if the solution involved jQuery AJAX, but vanilla JS would also do)
HTML:
<div class="panel panel-default">
<div class="panel-body">
<h2 id='div1'>test</h2>
<button id='ajax-btn'> Change Content</button>
</div>
</div>
Between I'm using Laravel 5.3 Framework so any solutions that involves built in framework features are also welcome!
You won't be able to send partial AJAX updates using PHP. Why don't you get all of the URLs from JavaScript and test them one by one.
$('a').each(function(){
var $anchor = $(this);
// Do Ajax call to check for this URL, directly to that page or your own PHP
$.ajax({
url: "../broken_links",
type: "get",
data: { url: $anchor.attr('href') },
success: function (result) {
$("#div1").load('http://website.dev/php');
// alert('works');
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
PHP
<?php
$url = $_GET['url'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
if (!preg_match('/404/', headers['http_code'])) {
echo "<p style='color:green'>" . $number . '. ' . $url . " - Works (Response: $check_url_status) </p>";
$good_array[] = $a;
} else {
echo "<p style='color:red'>" . $number . '. ' . $url . " - Broken (Response : $check_url_status) </p>";
}
?>

form Submit AJAX + PHP

On Sunday, I made a working script to send form data into my database and email with Mandrill.
Since Wednesday, my script doesn't work anymore. I'm trying to find the error but nothing fixed it.
I think I have an error in my PHP script but I don't know where and what.
Console log of my AJAX returns correct data but PHP doesn't do anything. It doesn't receive data.
There is my HTML
<form method="POST" id="formContact" action"#">
<input type="text" placeholder="Prénom" name="name" id="formName">
<input type="email" placeholder="Email" name="email" id="formEmail">
<input type="text" placeholder="Sujet" name="subject" id="formSubject">
<textarea cols="30" rows="10" placeholder="Ton message" name="message" id="formMessage"></textarea>
<button type="submit" id="formSubmit">Envoyer</button>
</form>
JavaScript
$(document).ready(function() {
$('#formContact').submit(function(event) {
var formData = {
'name' : $('#formName').val(),
'email' : $("#formEmail").val(),
'subject' : $("#formSubject").val(),
'message' : $("#formMessage").val()
};
$.ajax({
url: "commons/test.php",
type: "POST",
data: formData,
dataType: 'json',
encode: true
})
.done(function(data){
console.log(formData);
console.log(data);
if( ! data.success){
alert('Error');
}else{
alert('Success');
}
});
event.preventDefault();
});
});
PHP
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
include('../adm/dbconnect.php'); <- this work well
$data = array();
if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message']))
{
// var_dump($_POST['name']);
// var_dump($_POST['email']);
// var_dump($_POST['subject']);
// var_dump($_POST['message']);
$to = 'contact#sheguey.land';
$content = ''.$_POST['message'].'';
$subject = 'Nouveau message de '.$_POST['email'].' - Sheguey Land';
$from = ''.$_POST['email'].'';
$uri = 'https://mandrillapp.com/api/1.0/messages/send.json';
$api_key = 'my mandrill api key';
$content_text = strip_tags($content);
$postString = '{
"key": "' . $api_key . '",
"message": {
"html": "' . $content . '",
"text": "' . $content_text . '",
"subject": "' . $subject . '",
"from_email": "' . $from . '",
"from_name": "' . $from . '",
"to": [
{
"email": "' . $to . '",
"name": "' . $to . '"
}
],
"track_opens": true,
"track_clicks": true,
"auto_text": true,
"url_strip_qs": true,
"preserve_recipients": true
},
"async": false
}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
$result = curl_exec($ch);
$req = $bdd->prepare('INSERT INTO contactform (name, email, subject, message) VALUES( :name, :email, :subject, :message)');
$req->execute(array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'subject' => $_POST['subject'],
'message' => $_POST['message']
));
$data['success'] = true;
}else{
$data['success'] = false;
}
echo json_encode($data);
After two days of research (changing PHP script, AJAX script, etc), I don't understand why this don't work anymore.
you might need quotes in formData '"' + $('#formName').val() + '"'
What is the reasoning for the double single quotes?
$content = ''.$_POST['message'].'';
$subject = 'Nouveau message de '.$_POST['email'].' - Sheguey Land';
$from = ''.$_POST['email'].'';
I would just have done it as follows:
$content = (string)$_POST['message'];
$subject = "Nouveau message de ".$_POST['email']." - Sheguey Land";
$from = (string)$_POST['email'];
Does it var_dump the $_POST values after the if statement if you uncomment one?
var_dump($_POST); //as a whole
You may want to set error_reporting(E_ALL); to error_reporting(E_ALL & E_STRICT); which sometimes provides you with more information.
I have find the problem and fixed it.
A .htaccess file was in the same folder as the PHP file, i don't know why it was there. The script work.
Anyway thanks for you help ;)

Scraping data in dynamic sites

I'm trying to scrape data from our local government. What I want is address from kids adoption offices. Here, in Brazil, all adoptions go through the government. So I have the URL of one office, there are 2 or 3 thousands more. But if I can manage to get one, the others will be easy.
I made many attempts, bellow I show three.
The problem could be related to a Javascript (Ajax maybe) that refresh the page.
Note: I am not a PHP developer.
First attempt
echo '<html><head></head><body>';
echo '<h1>Scraper PHP GET 1</h1>';
echo ini_get("allow_url_fopen");
echo ini_get("allow_url_fopen");
// I used this url for test
//$url = 'http://www.portaldaadocao.com.br';
//This is the URL that I really want
$url = 'http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?transacao=CONSULTA&vara=2673';
$html = file_get_contents($url);
var_dump($html);
echo '</body></html>';
// Output
// 11
// Warning:
file_get_contents(http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?
transacao=CONSULTA&vara=2673) [function.file-get-contents]: failed to open stream: HTTP
request failed! HTTP/1.1 404 Not Found in /home/rsl/www/sc01_get.php on line 14
// bool(false)
Second attempt
echo '<html><head></head><body>';
echo '<h1>Scraper PHP CURL 3</h1>';
// I used this url for test
//$url = 'http://www.portaldaadocao.com.br';
//This is the URL that I really want
$url = 'http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?transacao=CONSULTA&vara=2673';
$curl = curl_init($url);
#curl_setopt($curl, CURLOPT_POSTFIELDS, "foo");
#curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
#curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");;
$html=#curl_exec($curl);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($curl);
echo "<br />cURL error:" . curl_error($curl);
exit;
}
else{
echo '<br>begin HTML[';
echo $html;
echo '<br>]end html ';
}
echo '</body></html>';
// Output
// 1
third attempt
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_REFERER, "http://www.windowsphone.com");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo '<html><head></head><body>';
echo '<h1>Scraper PHP CURL 5</h1>';
// I used this url for test
//$url = 'http://www.portaldaadocao.com.br';
//This is the URL that I really want
$url = 'http://www.cnj.jus.br/cna/Controle/ConsultaPublicaBuscaControle.php?transacao=CONSULTA&vara=2673';
$curl = curl_init($url);
#curl_setopt($curl, CURLOPT_POSTFIELDS, "foo");
#curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
#curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");;
$html=#curl($curl);
if (!$html) {
echo "<br />cURL error number:" .curl_errno($curl);
echo "<br />cURL error:" . curl_error($curl);
exit;
}
else{
echo '<br>begin HTML[';
echo $html;
echo '<br>]end html ';
}
echo '</body></html>';
// Output
// cURL error number:0
// cURL error:

Categories

Resources