Why can't these functions see my variable? - php

Why do I get this error:
Undefined variable key_2captcha
I run this code to pass a CAPTCHA to 2captcha server:
<?php
$id_Captcha=0;
$key_2captcha="key2captcha";
function send_captcha($base_file){
$ch = curl_init("http://2captcha.com/in.php");
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('method'=>"base64",
'key'=>$key_2captcha,
'numeric'=>1,
'max_len'=>1,
'body'=>$base_file,
'submit'=>'download and get the ID'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
return $postResult;
}
function getSolveCaptcha($id_captcha){
$c = curl_init("http://2captcha.com/res.php?key=".$key_2captcha."&action=get&id=".$id_captcha);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($c);
curl_close($c);
return $postResult;
}
?>
I run this code in XAMPP.

I thinks you have a variabile scope resolution problem.
If you want to use the variable into a generic function, you have to pass this variable as parameter in the signature of function.
Not use variable as global because is a bad practice, you have to make generic function so you have to use generic parameter.
Try this code:
<?php
$id_Captcha=0;
$key_2captcha="key2captcha";
function send_captcha($base_file, $key_2captcha){
$ch = curl_init("http://2captcha.com/in.php");
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('method'=>"base64",
'key'=>$key_2captcha,
'numeric'=>1,
'max_len'=>1,
'body'=>$base_file,
'submit'=>'download and get the ID'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
return $postResult;
}
function getSolveCaptcha($id_captcha, $key_2captcha){
$c = curl_init("http://2captcha.com/res.php?key=".$key_2captcha."&action=get&id=".$id_captcha);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($c);
curl_close($c);
return $postResult;
}
//Call Example
send_captcha($base_file, $key_2captcha);
?>

Use below code use $key_2captcha with global. in both function. read variable scope in PHP
function getSolveCaptcha($id_captcha){
global $key_2captcha;
$c = curl_init("http://2captcha.com/res.php?key=".$key_2captcha."&action=get&id=".$id_captcha);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($c);
curl_close($c);
return $postResult;
}

Use below code with $GLOBALS — References all variables available in global scope
<?php
$id_Captcha=0;
$key_2captcha="key2captcha";
function send_captcha($base_file){
$ch = curl_init("http://2captcha.com/in.php");
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('method'=>"base64",
'key'=>$GLOBALS['key_2captcha'],
'numeric'=>1,
'max_len'=>1,
'body'=>$base_file,
'submit'=>'download and get the ID'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
curl_close($ch);
return $postResult;
}
function getSolveCaptcha($id_captcha){
$c = curl_init("http://2captcha.com/res.php?key=".$GLOBALS['key_2captcha']."&action=get&id=".$id_captcha);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($c);
curl_close($c);
return $postResult;
}
?>
Ref PHP.net

Related

How get return request curl and paste it in a URL?

I have file-A.php where end in this way:
$testo= "L'abbonamento scade il: 25-08-2020 11:32 UTC";
print_r($testo);
return $testo;
then i have file-B.php where i have a function with curl for file-A.php
This function is:
function file_curl(){
$url_file= "https://file-A.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url_file);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result= curl_exec($ch); //esegui la chiamata
curl_close($ch);
$url= 'https://api.telegram.org/botxxx/sendMessage?chat_id=xxx&parse_mode=HTML&text='.$result;
file_get_contents($url);
return $result;
}
that $url and file_get_contentspaste inside Telegram the $result but i don't know why output is only
L
Why ?

file_get_contents equivalent of curl function

I have following curl program which is working fine.
$data="var1=$var1&var2=$var2";
$ch = curl_init("http://www.website.asmx/FetchData");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
I tried following code to get same data using file_get_contents but it returns false.
$data="var1=$var1&var2=$var2";
$result = file_get_contents("http://www.website.asmx/FetchData?=".$data);
var_dump($result);
exit();
What code will be equivalent to above curl program?
Notice the = at the End of FetchData in:
$result = json_decode(file_get_contents("http://www.website.asmx/FetchData?=".$data));
This should rather read:
//WITHOUT THE EQUAL SIGN AFTER FetchData...
$result = json_decode(file_get_contents("http://www.website.asmx/FetchData?".$data));

Share Counter only works on one post?

I have some code of which only works on one post, however i want it to work on all posts not just the one that has been listed i get the following error:
(Fatal error: Cannot redeclare class shareCount in counter/share-count.php on line 3)
loop.php (this is from my loop code for per post)
<?
require("counter/share-count.php");
$obj=new shareCount("http://google.com");
echo "Tweets: ".$obj->get_tweets();
echo "<br>Facebook: ".$obj->get_fb();
echo "<br>Google+: ".$obj->get_plusones();
?>
share-count.php (this is the file thats executable on request)
<?
class shareCount {
private $url,$timeout;
function __construct($url,$timeout=10) {
$this->url=rawurlencode($url);
$this->timeout=$timeout;
}
function get_tweets() {
$json_string = $this->file_get_contents_curl('http://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
$json = json_decode($json_string, true);
return isset($json['count'])?intval($json['count']):0;
}
function get_fb() {
$json_string = $this->file_get_contents_curl('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$this->url);
$json = json_decode($json_string, true);
return isset($json[0]['total_count'])?intval($json[0]['total_count']):0;
}
function get_plusones() {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"'.rawurldecode($this->url).'","source":"widget","userId":"#viewer","groupId":"#self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($curl);
curl_close ($curl);
$json = json_decode($curl_results, true);
return isset($json[0]['result']['metadata']['globalCounts']['count'])?intval( $json[0]['result']['metadata']['globalCounts']['count'] ):0;
}
private function file_get_contents_curl($url){
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
$cont = curl_exec($ch);
if(curl_error($ch))
{
die(curl_error($ch));
}
return $cont;
}
}
?>
Your included file counter/share-count.php defines the shareCount class every time it is included, which is why you are seeing the error. You can fix this by either using require_once() or check to see if the class is already defined using class_exists() and only defining it if the result is false.
Using require_once() to replace require():
require_once("counter/share-count.php");
// ... rest of your code
Using class_exists():
if ( ! class_exists( 'shareCount' ) ):
class shareCount{
// your class implementation
}
endif; // class_exists

Set cURL address via POST variable

I have been trying to use cURL in PHP with a dynamic address (i.e. set via a POST variable) but it isn't working!
<?php
$address = $_POST['address'];
//echo $address;
//$address = "http://twitter.com";
$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL, $address);
curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)) {
echo "Sorry, ".$address." are a bunch of poopy-heads.<p>";
}
else {
echo $buffer;
}
?>
I know the POST is working, because I can echo the $address variable and see the contents of $address that I sent.
OK, now since you know $_POST is working, let's look at your cURL.
This is the function I'm usually using. Maybe you can give it a try?
Code :
function getCurl(
$url,
$returntransfer=1, $httpproxytunnel=1, $referer=null,
$useragent=null, $header=null, $httpheader=null,
$proxy=null, $port=null
)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $returntransfer);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $httpproxytunnel);
if ($referer!=null)
curl_setopt($ch, CURLOPT_REFERER, $referer);
if ($useragent!=null)
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
if ($header!=null)
curl_setopt($ch, CURLOPT_HEADER, $header);
if ($httpheader!=null)
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
if ($proxy!=null)
curl_setopt($ch, CURLOPT_PROXY, $proxy);
if ($port!=null)
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
$result['DATA'] = curl_exec($ch);
$result['INFO'] = curl_getinfo($ch);
$result['ERROR'] = curl_error($ch);
return $result;
}
function getFile($url)
{
$data = getCurl($url);
return $data['DATA'];
}
And use it like this :
<?php
$fileContents = getFile("http://www.somedomain.com/a-path-to-your-file.html");
echo $fileContents;
?>
There was an input sanitation issue, in which there was a space in the $address variable's value.

curl_close will clean up the return result of curl_exec

I surely confirm the result of curl_exec will be cleaned up by curl_close.
I have to comment out the curl_close line to get the result.My php version is 5.3.8.
How do I get result with curl_close?
Here is my code
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
//curl_close($ch);
return $r;
}
It has no effect on the return value, as long as the data from curl_exec(); is stored in $r you can return as you like.
This works normally.
function curl_get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
$returnedValue = curl_get_contents($url); //Holds the contents
Edit as Marc B pointed out :
You don't need to do a curl close. PHP will clean up for you when the
function returns and $ch goes out-of-scope.
Hence there's no point of even closing it, but it shouldn't happen.

Categories