multipart/form-data php curl - php

I am using the OCR Service of i2ocr.com to convert an image to text..
In my project, I need to do this work automatically so I am using PHP to get the text of the image.
In the OCR website the postdata is contained in the form of multipart/form-data
Like this:
-----------------------------32642708628732\r\n
Content-Disposition: form-data; name="i2ocr_options"\r\n
\r\n
url\r\n
-----------------------------32642708628732\r\n
Content-Disposition: form-data; name="i2ocr_uploadedfile"\r\n
\r\n
\r\n
-----------------------------32642708629732\r\n
Content-Disposition: form-data; name="i2ocr_url"\r\n
\r\n
http://www.murraydata.co.uk/wp-content/uploads/2013/02/ocr-font-500x220.jpg\r\n
-----------------------------32642708628732\r\n
Content-Disposition: form-data; name="i2ocr_languages"\r\n
\r\n
gb,eng\r\n
-----------------------------32642708628732--\r\n
In PHP I am using
$ch = curl_init();
$dt = array();
$dt['i2ocr_options'] = 'url';
$dt['i2ocr_uploadedfile'] = '';
$dt['i2ocr_url'] = 'http://www.murraydata.co.uk/wp-content/uploads/2013/02/ocr-font-500x220.jpg';
$dt['i2ocr_languages'] = 'gb,eng';
curl_setopt($ch, CURLOPT_URL,"http://www.i2ocr.com/process_form");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:23.0) Gecko/20100101 Firefox/23.0");
curl_setopt($ch,CURLOPT_ENCODING,"gzip,deflate");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: multipart/form-data; boundary=---------------------------32642708628732"));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, "http://www.i2ocr.com/");
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$dt");
$html=curl_exec($ch);
print_r($html);
This code does not generate any errors, but I do not get any output either.
I need help getting the output from this curl request.

Like this:
<?php
function get($url, $refer, $ch)
{
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_POST, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt')); // cookie.txt
curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i586; de; rv:5.0) Gecko/20100101 Firefox/5.0');
curl_setopt ($ch, CURLOPT_REFERER, $refer);
$result= curl_exec($ch);
return $result;
}
function post($url, $refer, $parametros, $ch)
{
curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $parametros);
curl_setopt ($ch, CURLOPT_COOKIEJAR, realpath('cookie.txt')); // cookie.txt
curl_setopt ($ch, CURLOPT_COOKIEFILE, realpath('cookie.txt'));
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i586; de; rv:5.0) Gecko/20100101 Firefox/5.0');
curl_setopt ($ch, CURLOPT_REFERER, $refer);
$result= curl_exec($ch);
return $result;
}
function hazlo() {
$ch = curl_init();
/* STEP 1. visito la primera pagina para coger sus cookies */
get ("http://www.i2ocr.com/", "http://www.i2ocr.com/", $ch);
//STEP 2. Creo un array con los datos del post
$data = array(
'i2ocr_options' => 'url',
'i2ocr_uploadedfile' => '',
'i2ocr_url' => 'http://www.murraydata.co.uk/wp-content/uploads/2013/02/ocr-font- 500x220.jpg',
'i2ocr_languages' => 'gb,eng'
);
$data2 = http_build_query($data);
//STEP 3. Enviamos el el array en post
echo post ("http://www.i2ocr.com/process_form", "http://www.i2ocr.com/", $data2, $ch);
}
hazlo();
?>
use view source to see the response html, you can see the text of the image (sorry for my english). Works 100% :)

Related

How to redirect page after auto login with Curl keeping session?

I'm having a problem: after auto login using Curl PHP the site does not load css files and images.
My solution would then be to write the login session and redirect the page.
Code:
<?php
$cookie="cookie.txt";
$login_url = "https://online.hinode.com.br/loja_valida.asp";
$target_url="https://online.hinode.com.br/detalhes.asp?IdProduto=1294&ssp=1044632862SSP20171218HP180256";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$field['loja_consultor'] = '228621';
$field['estado'] = 'SP';
$field['acessar'] = 'Acessar';
$datafield = http_build_query($field);
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datafield);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $target_url);
$ket_qua = curl_exec($ch);
$ph = header ("location:42018010322060614551.htm", curl_exec($ch));
echo $ph;
curl_close($ch);
?>
Thanks in advance.
Adding a base tag could solve most of your concerns:CSS and images.
Try the following code:
<?php
$cookie="cookie.txt";
$login_url = "https://online.hinode.com.br/loja_valida.asp";
$target_url="https://online.hinode.com.br/detalhes.asp?IdProduto=1294&ssp=1044632862SSP20171218HP180256";
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$field['loja_consultor'] = '228621';
$field['estado'] = 'SP';
$field['acessar'] = 'Acessar';
$datafield = http_build_query($field);
$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datafield);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, $target_url);
$ket_qua = curl_exec($ch);
//add base URL
$baseUrlStr = '<head><base href="https://online.hinode.com.br/">';
$ket_qua=str_replace('<head>',$baseUrlStr,$ket_qua);
echo $ket_qua;
curl_close($ch);
?>

PHP cURL two pages are overlapped

Hi all I am using cURL for login to site and fetching some data after login. In my script I am able to get logged in but when I am displaying result it is getting both pages(e.g login and after login both in same variable).
Here is my code:
$username="abcd";
$password="123456";
$url="https://www.abcd.com/login";
$cookie="cookie.txt";
$url2 = "https://www.abcd.com/home";
$postdata = "username=".$username."&password=".$password;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/3.0.0.1");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
//echo $result;
// make second request
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POST, 0);
$data = curl_exec($ch);
var_dump($data);
curl_close($ch);
On
var_dump($data)
Output
I am getting both pages 1.)https://www.abcd.com/login and 2.)https://www.abcd.com/home in same variable.
Can anyone help me out!
Thank you.

Login using PHP and curl

I am trying to check login details via php curl for https://www.mercadolivre.com/jms/mlb/lgz/login?platform_id=mp&go=https://www.mercadopago.com/mlb/accountSummary.
Here is my code
$username="****#outlook.com";
$password="******";
$callbackerror= "https://www.mercadolivre.com/jms/mlb/lgz/login";
$go = "https://www.mercadopago.com/mlb/accountSummary";
$platform_id = "mp";
$site_id = "mlb";
$loginType = "DEFAULT";
$dps = "554738efe4b0aed09e93f37d";
$parent_url = "";
$arfRedirUrl = "";
$confirm = "Entrar";
$url="https://www.mercadolivre.com/jms/mlb/lgz/login/authenticate";
$postdata = "user_id=$username&password=$password&callback_error=$callbackerror&go=$go&platform_id=$platform_id&site_id=$site_id&loginType=$loginType&dps=$dps&parent_url=$parent_url&arfRedirUrl=$arfRedirUrl&confirm=$confirm";
//$postdata = "user_id=".$username."&password=".$password."&dps=".$dps;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
echo $result = curl_exec ($ch);
curl_close($ch);
It returns empty. What I am doing wrong?
Thanks

PHP + cURL: Error: The state information is invalid for this page and might be corrupted.

I try to login to
https://reg.viatoll.pl/PreLogin.aspx?language=po
with CURL. On error I should get "Error verification code" - in Polish: "Nieprawidlowy kod weryfikacyjny" but I get
Error: The state information is invalid for this page and might be corrupted. System.Web at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) at...
I don't know what I do wrong. I notify that this is sth with __VIEWSTATE and __EVENTVALIDATION. If I open this page in Chrome and change sth in __VIEWSTATE or __EVENTVALIDATION before post data I get the same error.
My code below:
<?php
require_once('simple_html_dom.php'); // http://simplehtmldom.sourceforge.net/manual.htm
$cookie="cookie2.txt";
$url = 'https://reg.viatoll.pl/PreLogin.aspx?language=po';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIE, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt ($ch, CURLOPT_REFERER, $url);
$result = curl_exec($ch);
$html = str_get_html($result);
$vs = $html->find('#__VIEWSTATE')[0]->value;
$ev = $html->find('#__EVENTVALIDATION')[0]->value;
// sending data
$txtVerificationCode='asdasdas';
$txtUserName="UserName";
$txtPassword="Haselko123";
$url='https://reg.viatoll.pl/PreLogin.aspx?language=po';
$postdata = "__LASTFOCUS=&__EVENTTARGET=btnLogin&__EVENTARGUMENT=&__VIEWSTATE=".$vs."&__EVENTVALIDATION=".$ev."&txtUserName=".$txtUserName."&txtPassword=".$txtPassword."&txtVerificationCode=".$txtVerificationCode;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt ($ch, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); // <-- add this line
curl_setopt ($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1);
$result = curl_exec ($ch);
echo "POST: " . $postdata . "<br><br><br>";
echo $result;
?>
Please anybody help :)

php cur login and display result

I have the following code to login to a remote website.
After executing the script i get the following error from the website : Technical Error
This is the result that i get from executing curl.
$username = $_POST['search']; $password = $_POST['pasword']; $ch = curl_init(); $postdata="&search=".$username."&password=".$password; curl_setopt ($ch, CURLOPT_URL,"https://payment.schibsted.no/login?client_id=5087dc1b421c7a0b79000000&response_type=code&redirect_uri=https%3A%2F%2Fwww.finn.no%2Ffinn%2FloginCallback%3FredirectKey%3D977170356717"); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); curl_setopt ($ch, CURLOPT_HEADER, true); curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); curl_setopt ($ch, CURLOPT_REFERER, "https://payment.schibsted.no/login?client_id=5087dc1b421c7a0b79000000&response_type=code&redirect_uri=https%3A%2F%2Fwww.finn.no%2Ffinn%2FloginCallback%3FredirectKey%3D977170356717"); curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt ($ch, CURLOPT_POST, 1); $result = curl_exec($ch); curl_setopt($ch, CURLOPT_URL, "menu link") ; $result2 = curl_exec($ch) ; echo $result2 ; curl_close($ch);
I cleaned up your code what had no line breaks and made it more understandable to some level.
//Variables
$username = $_POST['search'];
$password = $_POST['pasword'];
$cookie = $username.'.txt';
$postdata = "search=${username}&password=${password}";
$agent = 'Mozilla/5.0 (Windows; U; MSIE 7.0; Windows NT 6.0; en-US)';
//*not sure if all this is needed and I don't know why you set it as the referer*
$url = 'https://payment.schibsted.no/login?client_id=5087dc1b421c7a0b79000000&response_type=code&redirect_uri=https%3A%2F%2Fwww.finn.no%2Ffinn%2FloginCallback%3FredirectKey%3D977170356717';
// ---
//Curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);
// ---
echo $result;
//*Not sure what this is doing I found it at the bottom of your code*
//curl_setopt($ch, CURLOPT_URL, "menu link");
//$result2 = curl_exec($ch) ;
//echo $result2;
I don't think all this is necessary, also could you provide a little more information?

Categories