Creating cURL With Different POST DATA - php

I created a code algorithm that creates 10 different numbers. By using the PHP cURL I want to send POST requests to a website with those different 10 numbers, and then get the response bodies for them. Here is my example code for generating 10 different numbers:
function solver($aaa,$bbb,$number) {
$solo = substr($aaa,0,9);
$x=substr($solo,0,5);
$y=substr($solo,5,4);
if ($bbb == 0) {
for ($i = 1; $i <= $number ; $i++ ) {
$xx=$x+8*$i;
$dokuz=$xx.$y-1*$i;
$yeni=$dokuz;
echo $yeni."<br>";
}
}
}
solver("12345678912",0,10);
Here is the cURL:
<?
$url = "http:/example.com/solver.aspx";
$postdata = 'number'.'='.$yeni;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
preg_match_all("/id=\"__VIEWSTATE\" value=\"(.*?)\"/", $result, $arr_viewstate);
$viewstate = urlencode($arr_viewstate[1][0]);
preg_match_all("/id=\"__EVENTVALIDATION\" value=\"(.*?)\"/", $result, $arr_validation);
$eventvalidation = urlencode($arr_validation[1][0]);
preg_match_all("/id=\"__LASTFOCUS\" value=\"(.*?)\"/", $result, $arr_lastfocus);
$lastfocus = urlencode($arr_lastfocus[1][0]);
preg_match_all("/id=\"__EVENTTARGET\" value=\"(.*?)\"/", $result, $arr_eventtarget);
$eventtarget = urlencode($arr_eventtarget[1][0]);
preg_match_all("/id=\"__EVENTARGUMENT\" value=\"(.*?)\"/", $result, $arr_eventargument);
$eventargument = urlencode($arr_eventargument[1][0]);
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => '__EVENTTARGET='.$eventtarget.'&__EVENTARGUMENT='.$eventargument.'&__VIEWSTATE='.$viewstate.'&__EVENTVALIDATION='.$eventvalidation.'&__LASTFOCUS='.$lastfocus.'&'.$postdata.'&Submit=submit');
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$result = curl_exec ($ch);
preg_match("/<input name=\"Adi\" type=\"text\" value=\"(.*?)\" maxlength=\"25\" id=\"txtAdi\" disabled=\"disabled\" class=\"aspNetDisabled\" \/>/", $result, $adi);
$cikan = "<b>".$yeni."</b>"." "." ".$adi[1]." ";
print($cikan);
curl_close ($ch);
So, I am trying to make the cURL reply 10 times with those 10 different numbers. Can anyone help me about this issue?
Thank you,

Related

php - how can i getting a web page content with curl for this site

how can i getting a web page content with curl for this site:
divar . ir
I want try to get a page from a website with curl,But not work
I wrote the following code page 404 is displayed.
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
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$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;
}
$link="https://divar.ir/";
$res =get_web_page($link);
echo $res['content'];
Change CURLOPT_USERAGENT
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0",

wordpress 4.5 automatic Login with PHP CURL

I'm using php curl for automatic login into wordpress panel but code does n't work
<?php
$ch = curl_init();
$postData = array('log'=> 'admin',
'pwd' => '123',
'wp-submit' => '%D9%88%D8%B1%D9%88%D8%AF',
'redirect_to' => 'http://localhost/wp/wp-admin/',
'testcookie' => '1'
);
$cookie = basename('cookie.txt');
$opt = array(
CURLOPT_URL => 'http://localhost/wp/wp-login.php',
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => $cookie,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0',
CURLOPT_COOKIEFILE => $cookie
);
curl_setopt_array($ch,$opt);
$out = curl_exec($ch);
echo $out;
?>
wordpress login panel return :
ERROR: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress.
How do fix error
Your Login function :
function Login($data){
include_once($_SERVER['DOCUMENT_ROOT'].'/sbx/wp-config.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/sbx/wp-load.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/sbx/wp-includes/wp-db.php');
global $db;
global $json;
$credentials = array();
$secure_cookie='';
$username=$_POST['user_login'];
$password=$_POST['user_pass'];
if ( !empty($_POST['remember']) )
$credentials['remember'] = true;
else
$credentials['remember'] = false;
$secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials );
global $auth_secure_cookie;
$auth_secure_cookie = $secure_cookie;
add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);
$user = wp_authenticate($username, $password);
if ( ! is_wp_error($user) )
{
$json['success'] = true;
$json['data'] = $user;
} else {
$json['success'] = false;
$json['data'] = "Please Wrong Username And Password.";
}
Your Curl format:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"your_url");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "user_login=dcm&user_pass=123456&api=Login");
curl_exec ($curl);
curl_close ($curl);

Magento login curl

i'm trying to make a magento login with curl, what I need can be on the same server as magento
There is my code, currently not working:
$data = array(login[username]"=>"email", "login[password]"=>"password");
$url="https://example.com/customer/account/loginPost/";
$options=array();
$ch = curl_init();
$params = http_build_query($params);
$def_options = array(
CURLOPT_URL=>$url,
CURLOPT_SSL_VERIFYPEER=> 0,
CURLOPT_SSL_VERIFYHOST=> 0,
CURLOPT_FOLLOWLOCATION =>1,
CURLOPT_HEADER=>0,
CURLOPT_RETURNTRANSFER =>1,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)",
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR=> '/tmp/cookies.txt',
CURLOPT_COOKIEFILE=>'/tmp/cookies.txt',
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST=> true,
CURLOPT_POSTFIELDS=>$data,
CURLOPT_TIMEOUT => 100,
CURLOPT_VERBOSE=>TRUE,
CURLOPT_STDERR=>$verbose
);
$def_options += $options;
curl_setopt_array($ch,$def_options);
$response = trim(curl_exec($ch));
$info = curl_getinfo($ch);
curl_close($ch);
echo $response;
This simply redirects me on the login page without any error or successful login, it seems to do not send any POST data.
Any advice?
first. You've missed a quote sign at first line:
$data = array("login[username]"=>"email", "login[password]"=>"password");
^
second. change $params = http_build_query($params); to $data = http_build_query($data);
and you are ready to go.

How to use PHP curl to fetch www.yahoo.com page?

Try to write a simple crawler method. When I use PHP curl to get the www.yahoo.com page, I fetch nothing. How can I fetch the page?
My code is in the following.
public function getWebPage($url, $timeout = 120) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.19) Gecko/20081216 Ubuntu/8.04 (hardy) Firefox/2.0.0.19",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout,
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);
return $content;
}
The yahoo.com runs on secure socket layer. So add this cURL param to your existing set.
CURLOPT_SSL_VERIFYPEER => false,
and also disable the USERAGENT..
The working code.. (tested)
<?php
class A
{
public function getWebPage($url, $timeout = 120) {
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
//CURLOPT_USERAGENT => "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.19) Gecko/20081216 Ubuntu/8.04 (hardy) Firefox/2.0.0.19",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => $timeout,
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);
return $content;
}
}
$a = new A;
echo $a->getWebPage('www.yahoo.com');

Posting JSON to URL using PHP curl

I have the following JSON code:
{"username":"user1","password":"123456"}
That I need to pass to a url, lets say: http://api.mywebsite.com
I'm an extreme php newb, so I've been following a curl tutorial, but here is my current PHP code:
<?php
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle compressed
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;
}
?>
You might want to look into the CURLOPT_POSTFIELDS AND CURLOPT_POST options. These allow you to do a POST request and pass the data set into the CURLOPT_POSTFIELDS in the request.
Something in the lines of this:
$body = 'bar=1&foo=2&baz=3';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_POST, true);
curl_setopt ($c, CURLOPT_POSTFIELDS, $body);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
When you want to use normal GET Params:
$jsonString ='{"username":"user1","password":"123456"}';
$params = json_decode($jsonString);
$getParams = '';
$first = true;
foreach ($params as $key => $param){
if ($first){
$getParams .= '?';
$first = false;
} else{
$getParams .= '&';
}
$getParams .= $key .'=' .$param;
}
echo $getParams;
get_web_page($url . $getParams);

Categories