Function Inside Function Can't Find Variable - php

I'm trying to use the curl headerfunction opt from a class. I've tried putting the functions inside the class normally, but curl can't find them that way. So I put them inside the function I need it in. Here's the part of code that's applicable:
$ht = array();
$t = array();
function htWrite($stream,$buffer)
{
$ht[] = $buffer;
return strlen($buffer);
}
function tWrite($stream,$buffer)
{
$t[] = $buffer;
return strlen($buffer);
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'htWrite');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'tWrite');
When I put an echo statement in htWrite for the buffer, it echos out just fine. But if I do a print_r statement on $ht later, it says that it's empty. Further investigation says that it's creating its own $ht variable, because if I remove that line, $ht is null according to the function. So what can I do to fix this?

You need to look at how you can specify object methods as callbacks:
class Foo {
public function Bar() {
// do whatever
}
public function Test() {
$ch = curl_init('http://www.google.com');
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'Bar'));
}
}

Related

Extract links from a list of urls

I am trying to extract all the links from a set list of or urls in a text file and save the extracted links in another text file. I am trying to use the script below which was originally meant to extract Emails:
I changed the the email extract part
// preg_match_all('/([\w+\.]*\w+#[\w+\.]*\w+[\w+\-\w+]*\.\w+)/is', $sPageContent, $aResults);
to extract links like this:
preg_match_all("/a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>([^<]+|.*?)?<\/a>/is", $sPageContent, $aResults);
Here is the full code:
class getEmails
{
const EMAIL_STORAGE_FILE = 'links.txt';
public function __construct($sFilePath)
{
$aUrls = $this->getUrls($sFilePath);
foreach($aUrls as $sUrl) {
$rPage = $this->getContents($sUrl);
$this->getAndSaveEmails($rPage);
}
$this->removeDuplicate();
}
protected function getAndSaveEmails($sPageContent)
{
// preg_match_all('/([\w+\.]*\w+#[\w+\.]*\w+[\w+\-\w+]*\.\w+)/is', $sPageContent, $aResults);
preg_match_all("/a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>([^<]+|.*?)?<\/a>/is", $sPageContent, $aResults);
foreach($aResults[1] as $sCurrentEmail) {
file_put_contents(self::EMAIL_STORAGE_FILE, $sCurrentEmail . "\r\n", FILE_APPEND);
}
}
protected function getContents($sUrl)
{
if (function_exists('curl_init')) {
$rCh = curl_init();
curl_setopt($rCh, CURLOPT_URL, $sUrl);
curl_setopt($rCh, CURLOPT_HEADER, 0);
curl_setopt($rCh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($rCh, CURLOPT_FOLLOWLOCATION, 1);
$mResult = curl_exec($rCh);
curl_close($rCh);
unset($rCh);
return $mResult;
} else {
return file_get_contents($sUrl);
}
}
protected function getUrls($sFilePath)
{
return file($sFilePath);
}
protected function removeDuplicate()
{
$aEmails = file(self::EMAIL_STORAGE_FILE);
$aEmails = array_unique($aEmails);
file_put_contents(self::EMAIL_STORAGE_FILE, implode('', $aEmails));
}
}
new getEmails('sitemap_index.txt');
The problem i have with this is that it is supposed to get all links from a list of urls but it only scanned the first link and ignored the rest. I have 30 links that i want to extract from, how can i make the above code work?
you must using trim() at the url.. try add trim on your code
foreach($aUrls as $sUrl) {
$sUrl=trim($sUrl); //this
$rPage = $this->getContents($sUrl);
$this->getAndSaveEmails($rPage);
}

Laravel pass message from function to controller

I'm trying to get the error message returned by the API in my function to be shown in the controller, but it is returning null even if I set the error variable myself, i.e: $error = 'Error Message'
Function:
class ImportexportDomains {
public function add($input) {
$error = $data['message'];
return $error;
}
}
Controller:
public function store() {
$input = array_except(Input::all(), '_method');
$addnew = new ImportexportDomains;
$addnew->add($input);
dd($addnew);
}
$addnew
ImportexportDomains {#285 ▼
+error: null
}
public function add($input) {
$error = $data['message'];
return $error;
}
In the function above $input was never used, Whatsoever you pass later as parameter in that function will do nothing.
public function add($input) {
$error = "";
if($input == ""){
$error = "Not valid input";
}
return $error;
}
This might not be exactly what you want, but you can get idea from that.
You code have several errors. Looks like you didn't understood how a class and object works.
ImportexportDomains add() isn't a function. It's called method.
$data variable does not exists on add method context. (in fact, it doesn't exist anywhere)
You are doing dd() on the object, not on the add method response.
I know that you want to learn by trying yourself, but there's a nice community called Laracasts, that have some very well explained screencasts.
This two series will help you a lot to understand everything!
https://laracasts.com/series/php-for-beginners
https://laracasts.com/series/laravel-5-from-scratch
You need to pay a month to have access to the videos, but it's only $8!!!
Method
class ImportexportDomains {
public $error;
public function add($input) {
$userid = Settings::where('acc_id', Auth::user()->acc_id)->where('setting', 'resellerclub_id')->value('value');
$apikey = Settings::where('acc_id', Auth::user()->acc_id)->where('setting', 'resellerclub_key')->value('value');
$cust_email = Client::where('id', $input['client_id'])->value('email');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://httpapi.com/api/customers/details.json?auth-userid=$userid&api-key=$apikey&username=$cust_email");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$httpResponse = curl_exec($ch);
$data = json_decode($httpResponse, true);
if (isset($data['status'])) {
$error = $data['message'];
}
return $this->error = $error;
Controller
public function store() {
$input = array_except(Input::all(), '_method');
$addnew = new ImportexportDomains;
$addnew->add($input);
echo $addnew->error;
}

PHP Class Function returning a variable in a new file with-in a new function

I'm in need of some help with this insane issue i'm having
I have a class i made that i'm using Curl to obtain data from a URL and with that information $_GET's each variable a value.
So i have the class and i'm using a new file to get the $name of the item posted in a new function but every-time i put the phrased variable in the new function i get NULL here is my class in-short
class AppStore {
public $name;
public function getData() {
$requestUrl = self::APPSTORE_LOOKUP_URL . $this->appIdentity;
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $this->iTunesUserAgent);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
$parsedResponse = json_decode($response, true);
if ($parsedResponse['resultCount'] == 1)
{
$parsedResponse = $parsedResponse['results'][0];
$this->name = $parsedResponse['trackName'];
require_once('func.ini.php'); // new function to save images etc
} else {
throw new AppStoreService_Exception("mInvalid data returned from the AppStore Web Service. Check your app ID, and verify that the Appstore is currently up.");
}
}
}
// This is the func.ini.php code
echo $parsedResponse['trackName']; // Works here output equals a Name like Baraka etc
function save_image($img){
$dirs = "data/Apps/".$name; // Gets the name of the ID in the appIdentity so we can use CURL to download images and move the images to new folder else it'll fail and won't move
$path = $dirs."ScreenShots";
$fullp = basename($img);
$fullpath = "$path/$fullp";
$something = $dirs.'ScreenShots'
var_dump($parsedResponse['trackName']); // Null same if i echo it i get a white page etc
/* Sample Code
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_URL,$img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
*/
}
Basically if you can see if I try to add my class's variable to get the Name of the appIdentity in the new function, save_image it'll be null and i'm not sure how I'll be able to return the $this->name ($parsedResponse['trackName']) to be global for all use in new functions etc since my class is using the public getData() then the $this->name (getData()->name)
this only works out side any functions so if there is a function, the variable set to name will only have value above the function(s) i hope this is understandable.
Not sure if i need to make a return in the $this->name or what because i tried making a new public function in the class below the getName() and that comes out null as well thus i can't set public variables in a = statement because it'll catch(e) from try{} asking it expects a T_FUNCTION than T_VARIABLE i.e public $name = $name; // error public $name = 'Test'; // works '{$name}' don't work
class Test {
public $test = 'Help me out please!';
function GetMe() {
echo $this->test;
}
}
$open = new Test;
echo $open->GetMe(); // Outputs "Help me out please"
/* i can't do that with a variable though :( i.g $this->name / $parsedResponse['trackName'] */
If anyone can help me out I'd be much appreciated
Create an instance of your class and run the function that sets the name variable.
// Create an instance of AppName
$app_store = new AppName();
// Run the function
$app_store->getData();
You can then make use of the resulting $name variable created, within other functions outside of your class:
// By Making the instance global
function save_image()
{
global $app_store;
// Show the value of name
echo $app_store->name;
}
OR... by passing it to any functions that you want to make use of it
function save_image2($name_var)
{
echo $name_var;
}
// Called like
save_image2($app_store->name);

How to speed up class that checks links in HTML?

I have cobbled together a class that checks links. It works but it is slow:
The class basically parses a HTML string and returns all invalid links for href and src attributes. Here is how I use it:
$class = new Validurl(array('html' => file_get_contents('http://google.com')));
$invalid_links = $class->check_links();
print_r($invalid_links);
With HTML that has a lot of links it becomes really slow and I know it has to go through each link and follow it, but maybe someone with more experience can give me a few pointers on how to speed it up.
Here's the code:
class Validurl{
private $html = '';
public function __construct($params){
$this->html = $params['html'];
}
public function check_links(){
$invalid_links = array();
$all_links = $this->get_links();
foreach($all_links as $link){
if(!$this->is_valid_url($link['url'])){
array_push($invalid_links, $link);
}
}
return $invalid_links;
}
private function get_links() {
$xml = new DOMDocument();
#$xml->loadHTML($this->html);
$links = array();
foreach($xml->getElementsByTagName('a') as $link) {
$links[] = array('type' => 'url', 'url' => $link->getAttribute('href'), 'text' => $link->nodeValue);
}
foreach($xml->getElementsByTagName('img') as $link) {
$links[] = array('type' => 'img', 'url' => $link->getAttribute('src'));
}
return $links;
}
private function is_valid_url($url){
if ((strpos($url, "http")) === false) $url = "http://" . $url;
if (is_array(#get_headers($url))){
return true;
}else{
return false;
}
}
}
First of all I would not push the links and images into an array, and then iterate through the array, when you could directly iterate the results of getElementsByTagName(). You'd have to do it twice for <a> and <img> tags, but if you separate the checking logic into a function, you just call that for each round.
Second, get_headers() is slow, based on comments from the PHP manual page. You should rather use cUrl in some way like this (found in a comment on the same page):
function get_headers_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$r = curl_exec($ch);
$r = split("\n", $r);
return $r;
}
UPDATE: and yes, some kind of caching could also help, e.g. an SQLITE database with one table for the link and the result, and you could purge that db like each day.
You could cache the results (in DB, eg: a key-value store), so that your validator assumes that if a link was valid it's going to be valid for 24 hours or a week or something like that.

How use CURLOPT_WRITEFUNCTION when download a file by CURL

My Class for download file direct from a link:
MyClass{
function download($link){
......
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
function __writeFunction($curl, $data) {
return strlen($data);
}
}
I want know how to use CRULOPT_WRITEFUNCTION when download file.
Above code if i remove line:
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
Then it will run good, i can download that file.But if i use CURL_WRITEFUNCTION option i can't download file.
I know this is an old question, but maybe my answer will be of some help for you or someone else. Try this:
function get_write_function(){
return function($curl, $data){
return strlen($data);
}
}
I don't know exactly what you want to do, but with PHP 5.3, you can do a lot with the callback. What's really great about generating a function in this way is that the values passed through the 'use' keyword remain with the function afterward, kind of like constants.
function get_write_function($var){
$obj = $this;//access variables or functions within your class with the object variable
return function($curl, $data) use ($var, $obj) {
$len = strlen($data);
//just an example - you can come up with something better than this:
if ($len > $var){
return -1;//abort the download
} else {
$obj->do_something();//call a class function
return $len;
}
}
}
You can retrieve the function as a variable as follows:
function download($link){
......
$var = 5000;
$write_function = $this->get_write_function($var);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch, CURLOPT_WRITEFUNCTION , $write_function);
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
That was just an example. You can see how I used it here: Parallel cURL Request with WRITEFUNCTION Callback. I didn't actually test all of this code, so there may be minor errors. Let me know if you have problems, and I'll fix it.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://blog.ronnyristau.de/wp-content/uploads/2008/12/php.jpg');
$content = curl_exec($ch);
curl_close($ch);
$out = fopen('/tmp/out.png','w');
if($out){
fwrite($out, $content);
fclose($out);
}
Why do you use curl to download a file? Is there a special reason? You can simply use fopen and fread
I have written a small class for it.
<?php
class Utils_FileDownload {
private $source;
private $dest;
private $buffer;
private $overwrite;
public function __construct($source,$dest,$buffer=4096,$overwrite=false){
$this->source = $source;
$this->dest = $dest;
$this->buffer = $buffer;
$this->overwrite = $overwrite;
}
public function download(){
if($this->overwrite||!file_exists($this->dest)){
if(!is_dir(dirname($this->dest))){mkdir(dirname($this->dest),0755,true);}
if($this->source==""){
$resource = false;
Utils_Logging_Logger::getLogger()->log("source must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
}
else{ $resource = fopen($this->source,"rb"); }
if($this->source==""){
$dest = false;
Utils_Logging_Logger::getLogger()->log("destination must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
}
else{ $dest = fopen($this->dest,"wb"); }
if($resource!==false&&$dest!==false){
while(!feof($resource)){
$read = fread($resource,$this->buffer);
fwrite($dest,$read,$this->buffer);
}
chmod($this->dest,0644);
fclose($dest); fclose($resource);
return true;
}else{
return false;
}
}else{
return false;
}
}
}
It seems like cURL uses your function instead of writing to the request once CURLOPT_WRITEFUNCTION is specified.
So the correct solution would be :
MyClass{
function download($link){
......
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
function __writeFunction($curl, $data) {
echo $data;
return strlen($data);
}
}
This can also handle binary files as well.

Categories