No return from HTML using AJAX - php

I am trying to get the data using AJAX. My code is below. Basically what I am trying to do is: The first element contains HTML and the second element contains another JSON array with the data inside.
The 2nd element work find, but 1st element did not return any results.
If I write the code like this
$html = $cont[0][0];
It returns the results when I write code like this
$html = '<div>This is Html</div>';
Now I really did not know why this happen. If I echo the 1st one code style it show the data but did not pass it.
script Code
<script>
function get_data_from_rul() { //alert('ting');
var post_url = jQuery('#prod_url').val();
//alert('hmm');
jQuery.ajax({
url: 'ajax.php',
type: 'post',
data: {'post_url': post_url},
dataType: "json",
success: function(serverResponse) {
//location.reload();
//console.log(data);
jQuery('#prod_detail').val(serverResponse.html);
var data = JSON.parse(serverResponse.data);
jQuery('#meta_description').val(data.description);
jQuery('#meta_keyword').val(data.keywords);
jQuery('#prod_title').val(data.page_title);
}
});
}
</script>
HTML Code:
<input type="text" id="prod_url" name="prod_url" class="large" onkeyup="get_data_from_rul();" />
<br />
<textarea id="meta_description" rows="8" cols="90"></textarea><br />
<textarea id="meta_keyword" rows="8" cols="90"></textarea><br />
<textarea id="prod_title" rows="8" cols="90"></textarea><br />
<textarea id="prod_detail" rows="8" cols="90"></textarea>
ajax.php Code:
<?php
$url = $_REQUEST['post_url'];
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
$result = get_web_page( $url );
preg_match('/<title>(.+)<\/title>/',$result['content'],$matches[]);
preg_match('/<div id="specs-list">([^`]*?)<\/div>/',$result['content'],$cont[]);
$tags = get_meta_tags($url);
$keywords = $tags['keywords'];
$description = $tags['description'];
$page_title = $matches[0][1];
$html = $cont[0][0];
$data = json_encode(array('keywords'=>$keywords,'description'=>$description,'page_title'=>$page_title));
$response = array('html'=>$html, 'data'=>$data);
echo json_encode($response);

Why you have called twice json_encode in $data and in $response? You should only on $response to call json_encode.
Are you sure that you should be on first of array $cont[0][0] maybe not in $cont[0] instead?
preg_match('/<div id="specs-list">([^`]*?)<\/div>/',$result['content'],$cont); // Remove this [] braces, it is not neccessary, on code after you should check array
Part:
$tags = get_meta_tags($url);
$keywords = $tags['keywords'];
$description = $tags['description'];
$page_title = $matches[0][1];
if (isset($cont[0][0])
{
$html = $cont[0][0];
// echo($cont[0][0]); // Uncomment to check if you really have results.
}
// If you are not sure of returned result check with print_r and find array
// var_dump(isset($cont[0][1])); ....
$data = (array('keywords'=>$keywords,'description'=>$description,'page_title'=>$page_title));
$response = array('html'=>$html, 'data'=>$data);
echo json_encode($response);
Not sure, but hope this helps.

Related

how to send a Data Form including file, from PHP ContactForm7 with Curl

On my website, I use ContactForm7 to ask data and a file to users. Besides the automatic email sent by contactForm, I have a PHP function that retrieve the form data, and send them to my Node server to make some analysis.
My problem is sending the file with a POST to my server.
I can retrieve the data of the file, but I don't know how to pass them to the curl POST send.
This is my ContactForm module
<!-- WP CONTACT FORM -->
<div>
<div>
<label for="user_mail">Your email</label>
[email* user_mail]
</div>
<div>
<label for="file_invoice">Your file .xml</label>
[file file_invoice limit:1mb filetypes:xml|p7m]
</div>
[submit class:button class:default "SEND"]
</div>
This is the PHP code in functions.php
/*==============================
FUNCTIONS.PHP CONTACT-FORM HOOK
================================== */
add_action( 'wpcf7_before_send_mail', 'action_wpcf7_add_text_to_mail_body' );
function action_wpcf7_add_text_to_mail_body($contact_form)
{
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
$files = $submission->uploaded_files();
/* get file data */
$file_invoice = $files['file_invoice'][0];
$file_name = basename($file_invoice);
$file_content = file_get_contents($file_invoice);
/* retrieve text fields */
$user_mail = $data['user_mail'];
/* put them in an array */
$fields = array('user_mail' => $user_mail);
/* HOW I INSERT THE FILE TO BE SENT? */
$curl = curl_init();
$curlParams = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://....', // my node server
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data
);
curl_setopt_array($curl, $curlParams);
$resp = curl_exec($curl);
}
This is the content of the file vars. How Should I use them?:
$files = ( [file_invoice] => Array
([0] => "/customers/0/c/3/cashinvoice.it/httpd.www/wp-content/uploads/wpcf7_uploads/1229655618/ffffff.xml")
);
$file_name = "ffffff.xml";
$file_content = "<?xml version="1.0" encoding="utf-8"?> ......."; /* xml content */
And this is my Node Express server
var express = require('express');
var routes = express.Router();
var multiparty = require('connect-multiparty');
var multipartyMiddleware = multiparty();
routes.post('/my_route', multipartyMiddleware, function(req, res)
{
console.log(req.files); // should contains the file!!!!!
...
How should I use $file_invoice, $file_name, $file_content with Curl?
I ended up doing this in functions.php
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
$files = $submission->uploaded_files();
$file_invoice = $files['file_invoice'][0];
$cFile = "";
if (function_exists('curl_file_create'))
{ // php 5.5+
$cFile = curl_file_create($file_invoice);
}
else { $cFile = '#' . realpath($file_invoice); }
$fields = array(
'user_mail' => $user_mail,
'user_phone' => $user_phone,
'file' => $cFile
);
$curl = curl_init();
$curlParams = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://....',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $fields
);
curl_setopt_array($curl, $curlParams);
$resp = curl_exec($curl);
$responseStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

Extract string inside multi-quoted variable

I've a variable with multiple single quotes and want to extract a string of this.
My Code is:
$image['src'] = addslashes($image['src']);
preg_match('~src=["|\'](.*?)["|\']~', $image['src'], $matches);
$image['src'] = $matches[1];
$image['src'] contains this string:
tooltip_html(this, '<div style="display: block; width: 262px"><img src="https://url.com/var/galerie/15773_262.jpg"/></div>');
I thought all would be right but $image['src'] returns null. The addslashes method works fine and returns this:
tooltip_html(this, \'<div style="display: block; width: 262px"><img src="https://url.com/var/galerie/15773_262.jpg"/></div>\');
I don't get the problem in here, did I miss something?
=====UPDATE======
The whole code:
<?php
error_reporting(E_ALL);
header("Content-Type: application/json", true);
define('SITE', 'https://akipa-autohandel.autrado.de/');
include_once('simple_html_dom.php');
/**
* Create CDATA-Method for XML Output
*/
class SimpleXMLExtended extends SimpleXMLElement {
public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
/**
* Get a web file (HTML, XHTML, XML, image, etc.) from a URL. Return an
* array containing the HTTP server response header fields and content.
*/
function get_web_page( $url ) {
$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
$options = array(
CURLOPT_CUSTOMREQUEST =>"GET", //set request type post or get
CURLOPT_POST =>false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_COOKIEFILE =>"cookie.txt", //set cookie file
CURLOPT_COOKIEJAR =>"cookie.txt", //set cookie jar
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
if($content === FALSE) {
// when output is false it can't be used in str_get_html()
// output a proper error message in such cases
echo 'output error';
die(curl_error($ch));
}
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
function renderPage( $uri ) {
$rendering = get_web_page( $uri );
if ( $rendering['errno'] != 0 )
echo 'bad url, timeout, redirect loop';
if ( $rendering['http_code'] != 200 )
echo 'no page, no permissions, no service';
$content = $rendering['content'];
if(!empty($content)) {
$parsing = str_get_html($content);
}
return $parsing;
}
/**
* Get all current car data of the selected autrado site
*/
function models() {
$paramURI = SITE . 'schnellsuche.php?suche_hersteller=14&suche_modell=&suche_from=form&suche_action=suche&itemsperpage=500';
$content = renderPage($paramURI);
foreach ($content->find('tr[class*=fahrzeugliste]') as $auto) {
$item['src'] = $auto->find('a[onmouseover]', 0)->onmouseover;
preg_match('~src=["\'](.*?)["\']~', $item['src'], $matches);
echo $matches[1];
}
}
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
if((string) $action == 'test') {
$output = models();
json_encode($output);
}
}
?>
The content of $image['src'] is not as you wrote above. I've run now your script and the content is:
tooltip_html(this, '<div style="display: block; width: 262px"><img src="http://server12.autrado.de/autradogalerie_copy/var/galerie/127915_262.jpg" /></div>');
It will work if you add the following line before the preg_match:
$item['src']= html_entity_decode($item['src']);

Retrieve data from url and save in php

I am trying to retrieve the html from file get contents in php then save it to a php file so I can include it into my homepage.
Unfortunately my script isn't saving the data into the file. I also need to verwrite this data on a daily basis as it will be setup with a cron job.
Can anyone tell me where I am going wrong please? I am just learning php :-)
<?php
$richSnippets = file_get_contents('http://website.com/data');
$filename = 'reviews.txt';
$handle = fopen($filename,"x+");
$somecontent = echo $richSnippets;
fwrite($handle,$somecontent);
echo "Success";
fclose($handle);
?>
A couple of things,
http://website.com/data gets a 404 error, it doesn't exist.
Change your code to
$site = 'http://www.google.com';
$homepage = file_get_contents($site);
$filename = 'reviews.txt';
$handle = fopen($filename,"w");
fwrite($handle,$homepage);
echo "Success";
fclose($handle);
Remove $somecontent = echo $richSnippets; it doesn't do anything.
if you have the proper permissions it should work.
Be sure that your pointing to an existing webpage.
Edit
When cURL is enabled you can use the following function
function get_web_page( $url ){
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
curl_close( $ch );
return $content;
}
Now change
$homepage = file_get_contents($site);
in to
$homepage = get_web_page($site);
You should use / instead of ****
$homepage = file_get_contents('http://website.com/data');
Also this part
$somecontent = echo $richSnippets;
I don't see $richSnippets above... it's probably not declared?
You probably want to do this:
fwrite($handle,$homepage);

Regex to filter certain type of url

I am writing and learning a simple crawler script to read all links within a website. I have a problem with the pattern, and I do not understand why this is not working.
The links looks like this in the sourcecode of the website:
Handlauf Holz
My pattern and function looks like this:
preg_match_all( '/ObjectPath.*"/', $contentrow, $output, PREG_SET_ORDER
It works for the first half, but after that it breaks the output. Here a sample of the output where its broken:
ObjectPath=/Shops/15456062/Categories">-GESAMTANGEBOT-Handläufe
ObjectPath=/Shops/15456062/Products/%22Handlauf%20Edelstahl%20DS01%22/SubProducts/%22Handlauf%20Edelstahl%20DS%2001%20014%22&#ProductRatings"
ObjectPath=/Shops/15456062/Categories/CustomerInformation"
ObjectPath=/Shops/15456062/Products/%22Handlauf%20Edelstahl%20DS01%22/SubProducts/%22Handlauf%20Edelstahl%20DS%2001%20014%22&ChangeAction=SelectSubProduct" method="post"
The part in the sourcecode, where the part was get from, looks like this:
<a class="BreadcrumbItem" href="?ObjectPath=/Shops/345456456/Categories">-GESAMTANGEBOT-</a><a class="BreadcrumbItem" href="?ObjectPath=/Shops/1234346q/Categories/Handlauf">Handläufe</a><a class="BreadcrumbItem" href="?ObjectPath=/Shops/15456062/Categories/Handlauf/%22Handlauf%20Edelstahl%22">Handläufe Edelstahl</a>
I do not understand, why the part -GESAMTANGEBOT- is taken into the pattern. the " should finish it?
Thank you!
Here the complete Script:
<?php
header('Content-Type: text/html; charset=utf-8');
function getPage($url){
// Prüfung ob cURL installiert ist?
if (!function_exists('curl_init')){
die('Curl not initialed');
}
// Array mit den cURL-Einstellungen
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_ENCODING => "",
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_MAXREDIRS => 10
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
$url = "http:/domain.com/epages/23455467.sf/de_DE/?ObjectPath=/Shops/15456062/Products/%22Handlauf%20Edelstahl%20DS01%22/SubProducts/%22Handlauf%20Edelstahl%20DS%2001%20014%22";
$domain = 'http://www.domain.com/epages/452563456.sf/de_DE/?';
$content = getPage($url);
$i=0;
foreach ($content as $contentrow) {
//go through content and look for links
if (preg_match_all( '/ObjectPath(.*)"/', $contentrow, $output, PREG_SET_ORDER )) {
$i++;
echo '<h1>'.$i.'</h1>';
foreach ($output as $row) {
$url= $domain.$row[0];
//echo ''.$url.'';
echo $url;
echo '<br /><h2>onerow</h2><br />';
}
}
}
//print_r($content);
And I forgot to mention, I receive this warning above the output:
Warning: preg_match_all() expects parameter 2 to be string, array given in C:\xampp\htdocs\scripts\readratings.php on line 48
If I understood correctly, you have something like:
<a class="BreadcrumbItem" href="?ObjectPath=/Shops/345456456/Categories">-GESAMTANGEBOT-</a><a class="BreadcrumbItem" href="?ObjectPath=/Shops/1234346q/Categories/Handlauf">Handläufe</a><a class="BreadcrumbItem" href="?ObjectPath=/Shops/15456062/Categories/Handlauf/%22Handlauf%20Edelstahl%22">Handläufe Edelstahl</a>
And you want all those parts:
ObjectPath=/Shops/345456456/Categories
ObjectPath=/Shops/1234346q/Categories/Handlauf
ObjectPath=/Shops/15456062/Categories/Handlauf/%22Handlauf%20Edelstahl%22
While I don't know why you have this strange output, you should be able to get what you want with a lazy operator. This should do what you want:
/ObjectPath(.*?)"/
as it will stop at the first ".
In this case, it's equivalent to:
/ObjectPath([^"]*)"/
though it's not in a general case.
use
$contentrow = 'Handlauf Holz ';
preg_match_all( '/ObjectPath(.*)"/', $contentrow, $output, PREG_SET_ORDER);
print_r($output);
output:
Array
(
[0] => Array
(
[0] => ObjectPath=/Shops/154567062/Categories/Handlauf/%22Handlauf%20Holz%22"
[1] => =/Shops/154567062/Categories/Handlauf/%22Handlauf%20Holz%22
)
)

PHP: how to load file from different server as string?

I am trying to load an XML file from a different domain name as a string. All I want is an array of the text within the < title >< /title > tags of the xml file, so I am thinking since I am using php4 the easiest way would be to do a regex on it to get them. Can someone explain how to load the XML as a string? Thanks!
You could use cURL like the example below. I should add that regex-based XML parsing is generally not a good idea, and you may be better off using a real parser, especially if it gets any more complicated.
You may also want to add some regex modifiers to make it work across multiple lines etc., but I assume the question is more about fetching the content into a string.
<?php
$curl = curl_init('http://www.example.com');
//make content be returned by curl_exec rather than being printed immediately
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if ($result !== false) {
if (preg_match('|<title>(.*)</title>|i', $result, $matches)) {
echo "Title is '{$matches[1]}'";
} else {
//did not find the title
}
} else {
//request failed
die (curl_error($curl));
}
first use
file_get_contents('http://www.example.com/');
to get the file,
insert in to var.
after parse the xml
the link is
http://php.net/manual/en/function.xml-parse.php
have example in the comments
If you're loading well-formed xml, skip the character-based parsing, and use the DOM functions:
$d = new DOMDocument;
$d->load("http://url/file.xml");
$titles = $d->getElementsByTagName('title');
if ($titles) {
echo $titles->item(0)->nodeValue;
}
If you can't use DOMDocument::load() due to how php is set up, the use curl to grab the file and then do:
$d = new DOMDocument;
$d->loadXML($grabbedfile);
...
I have this function as a snippet:
function getHTML($url) {
if($url == false || empty($url)) return false;
$options = array(
CURLOPT_URL => $url, // URL of the page
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 3, // stop after 3 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
//Ending all that cURL mess...
//Removing linebreaks,multiple whitespace and tabs for easier Regexing
$content = str_replace(array("\n", "\r", "\t", "\o", "\xOB"), '', $content);
$content = preg_replace('/\s\s+/', ' ', $content);
$this->profilehtml = $content;
return $content;
}
That returns the HTML with no linebreaks, tabs, multiple spaces, etc, only 1 line.
So now you do this preg_match:
$html = getHTML($url)
preg_match('|<title>(.*)</title>|iUsm',$html,$matches);
and $matches[1] will have the info you need.

Categories