Parse web page without save local? - php

Sorry my English a little. I am using CURL because web page is required this function. I don't get file_get_contents of page. How to parse page without page save? (fopen,fwrite)
<?PHP
function fileGet($url, $timeout = 55, $ref=true){
$useri = $_SERVER['HTTP_USER_AGENT'];
#set_time_limit($timeout);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
curl_setopt($curl, CURLOPT_USERAGENT, $useri);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER,$url);
$data = curl_exec($curl);
curl_close($curl);
// Save Page Start
$fp = fopen('data.html', 'w');
fwrite($fp, $data);
fclose($fp);
// Save Page End
return $data;
}
// Start Code
fileGet("http://www.example.com",10); // Start Function
$html = file_get_html('data.html'); // Open Saved Page In Local
foreach($html->find('div.columns') as $article) {
// Events.....
mysql_query("Insert Query");
}
// End Code
?>

<?PHP
function fileGet($url, $timeout = 55, $ref=true){
$useri = $_SERVER['HTTP_USER_AGENT'];
#set_time_limit($timeout);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
curl_setopt($curl, CURLOPT_USERAGENT, $useri);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER,$url);
$data = curl_exec($curl);
return $data;
}
// Start Code
$html = str_get_html(fileGet("http://www.example.com",10));
foreach($html->find('div.columns') as $article) {
// Events.....
mysql_query("Insert Query");
}
// End Code
?>

Related

Sending information to 3 random websites from a list of 6 using curl

I have a mailer that sends solo ads to a small network of similar sites, but I want to change it so that the solo is sent to 3 random sites from the list, not to all of them. I only have very basic PHP knowledge and have spent many hours trying to work this out and checking forums. I assume I'll have to set up an array, but don't know where to start. Here is the relevant coding of the file:
<?php
session_start();
include "../config.php";
include "../header.php";
include "../style.php";
error_reporting(E_ALL);
$id = $_POST['id'];
if($_SESSION['admin'] != $adminpw) {
?><table>
<tr>
<td width="15%" valign=top><br>
<? include("adminnavigation.php"); ?>
</td>
<td valign="top" align="center"><br><br> <?
echo "<font size=2 face='$fonttype' color='$fontcolour'><p><b><center>";
if($_POST['submit'] == "Delete") {
foreach($id as $each) {
mysql_query ("update solos set added=0, approved=0, subject='', adbody='', url='' where id=".$each);
}
echo "<p><center>The solos have been sent back to the users.</p></center>";
} else {
error_reporting(E_ALL);
foreach($id as $each) {
mysql_query ("update solos set approved=1, date='".time()."' where id=".$each);
$sql = mysql_query("SELECT * FROM solos WHERE id=".$each);
$info = mysql_fetch_array($sql);
// Work out the data
$wannasay = array (
"subject" => $info['subject'],
"adbody" => $info['adbody'],
"userid" => $info['userid'],
"url" => $domain."/solo_click.php?id=".$info['id']
);
$dataels = array();
foreach (array_keys($wannasay) as $thiskey) {
array_push($dataels,urlencode($thiskey) ."=".
urlencode($wannasay[$thiskey]));
}
$data = implode("&",$dataels);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.WEBSITE1.COM/admin/solo_curl.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.WEBSITE2.com/admin/solo_curl.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.WEBSITE3.com/admin/solo_curl.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.WEBSITE4.com/admin/solo_curl.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.WEBSITE5.com/admin/solo_curl.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.WEBSITE6.com/admin/solo_curl.php");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
This is the file for approving the solo ad in admin, and the information is then sent to the relevant file at the other sites for placement in the database. I want the solo ad sent to 3 random sites every time I approve a solo ad, so really only need the coding to make this happen.
Thanks
Martin
Just after $data = implode("&",$dataels); add the following:
$website_array = array(
0 => 'website1.com/admin/solo_curl.php',
1 => 'website2.com/admin/solo_curl.php',
2 => 'website3.com/admin/solo_curl.php',
3 => 'website4.com/admin/solo_curl.php',
4 => 'website5.com/admin/solo_curl.php',
5 => 'website6.com/admin/solo_curl.php'
);
$rand_keys = array_rand($website_array, 3);
foreach ($rand_keys as $key) {
$website_to_send_to = $website_array[$key];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $website_to_send_to);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($curl);
curl_close($curl);
}
And remove all your cURL sections.

openssl_get_publickey() and curl "unable to use client certificate"

I got following code..
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$keystore = '/var/www/html/key.pem';
$url = 'https://myurl';
$keystorepassword = '123';
$key2 = "/var/www/html/public.pem";
$handler = fopen($key2, "r");
$kkey = fread($handler, 8192);
fclose($handler);
$pubkey = openssl_get_publickey($kkey);
openssl_free_key($pubkey);
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($curl, CURLOPT_SSLVERSION,3);
curl_setopt($curl, CURLOPT_SSLCERT, $keystore);
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keystorepassword);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'data');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result =curl_exec ($curl);
var_dump($result);
if(curl_error($curl)){
$result = curl_error($curl);
var_dump($result);
}
curl_close ($curl);
?>
And it returns "unable to use client certificate (no key found or wrong pass phrase?)".
If i comment line "$pubkey = openssl_get_publickey($kkey);", then curl works fine.
Is there any chance, that "something" stays in memory and curl uses it?

curl is not collecting all the cookies from external site

for some reason , its not collecting all the cookies, its not collecting the password hash or the member id , im not sure why its not setting those since its getting the others, am i doing somthing wrong with my coding, this is my first time using curl
this is the information in the cookie.txt file
<?php
//init curl
function curl_file_get_contents($url){
$username = 'user#hotmail.com';
$password = 'mypass';
$loginUrl = 'http://forums.zybez.net/index.php?app=curseauth&module=global&section=login';
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username . '&pass=' . $password);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_REFERER, 'http://forums.zybez.net/runescape-2007-prices/282-law+rune');
$content = curl_exec($ch);
curl_close($ch);
file_put_contents('~/download.zip', $content);
$curl = curl_init();
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_REFERER, $loginUrl);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$contents = curl_exec($curl);
return $contents;
curl_close($curl);
}
function get_input_tags($html){
$post_data = array();
// a new dom object
$dom = new DomDocument;
//load the html into the object
$dom->loadHTML($html);
//discard white space
$dom->preserveWhiteSpace = false;
//all input tags as a list
$input_tags = $dom->getElementsByTagName('input');
//get all rows from the table
for ($i = 0; $i < $input_tags->length; $i++) {
if (is_object($input_tags->item($i))) {
$name = $value = '';
$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
if (is_object($name_o)) {
$name = $name_o->value;
$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
if (is_object($value_o)) {
$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
}
$post_data[$name] = $value;
}
}
}
return $post_data;
}
/*
Usage
*/
error_reporting(~E_WARNING);
function getauth(){
$html = curl_file_get_contents("http://forums.zybez.net/runescape-2007-prices/282-law+rune");
echo "<pre>";
$auth1 = (get_input_tags($html));
$auth = $auth1["auth"];
print_r($auth1);
}
getauth();
?>

I am using curl to download profile photo. Is that right method?

function getFacebookPhoto($img,$fullpath) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $img);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
$user_curl_image = curl_exec($curl);
curl_close($curl);
if(file_exists($fullpath)){
#unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $user_curl_image);
fclose($fp);
}
This is the script i use to download photos.
Is it rightway to do it?
I use this function inside facebook apps.

Twitter XML feed

I have just coded twitter feed for my statuses fetching the xml. I was wondering if there is any link also for XML of mentions and hashtags
For twitter mentions:
function curlRequest($url, $post) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// we are not using the $post variable yet, but stay tuned!
if ($post) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
return curl_exec($curl);
}
//then call this function as:
$contents= curlRequest(“http://twitter.com/statuses/mentions.xml”, FALSE);
$xml= new SimpleXMLElement($contents);
foreach($xml->status as $status) {
echo "<li>$status->text</li>";
}

Categories