Make 2 cURL requets with $_POST - php

I am trying to do 2 requets in cURL. The first request is login me in, and I conserve a txt file to make the second requests when still loged.
The first part work like a charm, the second part doesn't work.
There is my code.
$lien = 'https://mysite.com/login';
$postfields = array(
'username' => 'test',
'password' => 'test'
);
$path_cookie = 'connexion.txt';
if (!file_exists(realpath($path_cookie))) touch($path_cookie);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath($path_cookie));
$return = curl_exec($curl);
echo($return);
curl_close($curl);
$lien2 = 'https://mysite.com/myform';
$postfields2 = array(
'data1' => 'test123',
'data2' => 'Account',
'sort' => '3',
'fileFormat' => '0',
'timezone' => 'Eastern+Standard+Time',
'zeroRated' => 'true',
'startDate' => '2013-05-01',
'endDate' => '2013-05-31'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields2);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath($path_cookie));
$return = curl_exec($curl);
echo $return;
curl_close($curl);
unlink(realpath($path_cookie));
I am using the second part to $_post data to execute a form. The form processing takes place in the same page...
My connexion still on, but I don't know, the server is shooting me an error like :
An exception occurred while processing your request. We recorded the exception..
It doesn't help me. Any one have an idea?
Thanks.

You have maybe a problem with SSL at this point.
Try to add these options:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

finally I receive an answer from the support team. The "Eastern+Standard+Time" should be "Eastern Standard Time", the server will automaticly escape those string ... Thaks to every body for your help !

Related

php curl login not work, code tested with other site

I have a problem with curl remote login, i don't know what the issue , tried the code with other site working but with my customer website not working
function get_cURL() {
$url = "https://qualitycarrentalltd.com/admin/login.php";
$ch = curl_init($url);
$credentials = "user=myusername&pass=mypassword&Submit=Login";
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $credentials);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language: en-us,en;q=0.5','Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7','Keep-Alive: 115','Connection: keep-alive'));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie-name.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie-name.txt');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
var_dump(get_cURL());
another code
$url = 'https://qualitycarrentalltd.com/admin/login.php';
$data = array('user' => 'myusername', 'pass' => 'mypassword', 'Submit' => 'Login');
// use key 'http' even if you send the request to https://...
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
print_r($result);
Why login not working? it take days from me without found solution, Can someone help with fix Please

What is wrong with my paypal recurring code

I did everything right, all seem legit, but i don't see any payments made by my dummy user to the merchant:
Is it something in my code:
creating plan:
$req = array(
'USER' => 'bennyrefaelov-facilitator_api1.gmail.com',
'PWD' => 'YWKQ3M3NXBTZQ78U',
'SIGNATURE' => 'AFcWxV21C7fd0v3bYYYRCpSSRl31AuVEFUpBu6N8yXkmFksuwwSiQOW8',
'VERSION' => '98.0',
'METHOD' => 'SetExpressCheckout',
'PAYMENTREQUEST_0_PAYMENTACTION' => 'Mark',
'REQCONFIRMSHIPPING'=>'0',
'NOSHIPPING'=>'1',
'ALLOWNOTE'=>'0',
'SOLUTIONTYPE'=>'Sole',
'LANDINGPAGE'=>'Billing',
'BRANDNAME'=>'MisterSurvey',
'PAYMENTREQUEST_0_AMT'=>'100.00',
'MAXAMT' => '100.00',
'L_BILLINGAGREEMENTDESCRIPTION0' => 'Pay up',
'PAYMENTREQUEST_0_TAXAMT'=>'0',
'PAYMENTREQUEST_0_ITEMAMT'=>'100.00',
'L_BILLINGTYPE0' => 'RecurringPayments',
'PAYMENTREQUEST_0_DESC'=>'An awesome package',
'PAYMENTREQUEST_0_CUSTOM'=>'This is just for fun',
'PAYMENTREQUEST_0_CURRENCYCODE'=>'USD',
'L_PAYMENTREQUEST_0_NUMBER0'=>'itemid1',
'L_PAYMENTREQUEST_0_NAME0'=>'MyItem1',
'L_PAYMENTREQUEST_0_DESC0'=>'basic package',
'L_PAYMENTREQUEST_0_QTY0'=>'1',
'L_PAYMENTREQUEST_0_AMT0'=>'100.00',
'L_PAYMENTREQUEST_0_TAXAMT0'=>'0',
'RETURNURL'=>'http://mistersurveylocal.com:8080/#/pricing',
'CANCELURL'=>'http://mistersurveylocal.com:8080/#/'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
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);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($req));
$return = curl_exec($ch);
curl_close($ch);
excute plan (after user agrees)
$token = Input::get('token');
$payerId = Input::get('payerId');
$req = array(
'USER' => 'bennyrefaelov-facilitator_api1.gmail.com',
'PWD' => 'YWKQ3M3NXBTZQ78U',
'SIGNATURE' => 'AFcWxV21C7fd0v3bYYYRCpSSRl31AuVEFUpBu6N8yXkmFksuwwSiQOW8',
'VERSION' => '98.0',
'METHOD' => 'CreateRecurringPaymentsProfile',
'TOKEN'=>$token,
'payerid'=>$payerId,
'PROFILESTARTDATE' =>date('Y-m-d\TH:i:s\Z'),
'TOTALBILLINGCYCLES'=>'12',
'DESC'=>'Pay up',
'BILLINGPERIOD'=>'Month',
'BILLINGFREQUENCY'=>'1',
'AMT'=>'100',
'CURRENCYCODE'=>'USD',
'COUNTRYCODE'=>'US',
'MAXFAILEDPAYMENTS'=>'3'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp');
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);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($req));
$return = curl_exec($ch);
curl_close($ch);
return urldecode($return);
It says it's active, and verified, and the start date is at this moment, why there is no payment made?
is there something missing?
I had the exact same problem. As it turns out, it was an issue with the Sandbox being very slow. After 2 days of troubleshooting and debugging, all the transactions completed and I received all IPN messages related to them.
It's unfortunate, but the solution is simply to wait for them to process. Hopefully live will be much faster.

Php Curl add string file as a post field

I am using mailGun web API and ran into the issue of adding inline files.
Our software creates an image and passes it around as a string. I want to inline that image
The problem that I have is that php curl takes in a file pointer, and not an actual file. I want to avoid writing a tmp file if possible as we have many process that work on the server and would not want to send a bad email
Thanks in advance
MailGun inline Sample:http://documentation.mailgun.net/user_manual.html#inline-image
Code sample that I am using:
function send_inline_image($image) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('from' => 'Excited User <me#samples.mailgun.org>',
'to' => 'sergeyo#profista.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!',
'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
'inline' => $image))
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$image = 'Some image string that we have generated'
send_inline_image($image)
You need to change only the inline parameter of array. I have done it and its works. Inline parameter should be an array instead of string image path. You can do it like this:
function send_inline_image($image) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'api:key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, 'https://api.mailgun.net/v2/samples.mailgun.org/messages');
curl_setopt($ch,
CURLOPT_POSTFIELDS,
array('from' => 'Excited User <me#samples.mailgun.org>',
'to' => 'sergeyo#profista.com',
'subject' => 'Hello',
'text' => 'Testing some Mailgun awesomness!',
'html' => '<html>Inline image: <img src="cid:test.jpg"></html>',
'inline' => array($image)//Use array instead of $image
))
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$image = 'Some image string that we have generated'
send_inline_image($image)
Look at comment "Use array instead of $image"

cURL post not working PHP

I'm having trouble using cURL for a specific page.
A live code working: http://svgen.com/jupiter.php
Here is my code:
$url = 'https://uspdigital.usp.br/jupiterweb/autenticar';
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere");
curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_exec($ch);
curl_close($ch);
Although I had used the same url and post data, file_get_contents worked:
$options = array('http' => array('method' => 'POST','content' => http_build_query($data)));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Someone could help me?
Thanks.
Most probably it is the SSL verification problem.
Add
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Also if you use CURLOPT_PROTOCOLS option, it should be HTTPS since you are posting to a secure url
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); // you currently have http
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
should have been
$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'));
It automatically url encodes your query string as well and is safer than manual methods..
make your post data as:
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$postData = "";
foreach( $data as $key => $val ) {
$postData .=$key."=".$val."&";
}
$postData = rtrim($postData, "&");
and change:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
Try these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS
This makes sure the resource you're "curling" has a valid SSL certificate. It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0).
You're on a secure connection, why are you using :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
Use instead :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);

Simple HTML Dom and Curl

Hi is curl the best method for using POST with cookie and also navigated to another page for scrapping? I'm using the coding below and I can't get it to work.
include('simple_html_dom.php');
$data = array(
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => '%2FwEPDwUKLTcyODA2ODEwMGRk',
'Myname' => 'justdev12345',
'Mypassword' => '12345',
'idLogin' => 'Login',
'tmplang2' => '6',
'fm' => '',
'jc' => '',
'LoginRef' => ''
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.site.com/mainframe.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$output = new simple_html_dom();
$output = file_get_html('http://profiles.site.com/profile_900.aspx?AccountID=ShopCartUpdate');
print $output;
Your code is fine up until the end where you throw away the curl response and load the url with simple-html-dom. If you want to use the curl response it should look like this:
$html = str_get_html($output);
$html->find('title');
Otherwise you should probably remove all the curl code.

Categories