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);
Related
Essentially the following script is working fine when in local host but fails to load (Timeout) anytime loaded from a web server, is their something incorrectly written? I know it's not the end point api url that's the issue, since it works on local. Please see below, wondering if it's caused by curl_setopt:
$id = "dhl";
$number = "039103913";
// Parameters setting
$key = 'key';
$secret = 'scret';
$param = array (
'id' => $id,
'number' => $number,
'phone' => '',
'ship_from' => '',
'ship_to' => '',
'area_show' => 1,
'order' => 'desc'
);
// Request Json
$json = json_encode($param, JSON_UNESCAPED_UNICODE);
$signature = strtoupper(md5($json.$key.$secret));
$url = 'https://www.kd100.com/api/v1/tracking/realtime';
$headers = array (
'Content-Type:application/json',
'API-Key:'.$key,
'signature:'.$signature,
'Content-Length:'.strlen($json)
);
// Send post request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$data = json_decode($result, true);
echo json_encode($data);
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
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 !
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.
I need to read the size of a file but the server is forcing me to download it first.
I note one of the response headers is Content-Type: application/force-download and that seems to bypass my curl inputs...
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_TIMEOUT,1000);
curl_exec($ch);
$bytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
Any ideas?
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
These two lines reset CURLOPT_NOBODY.
CURLOPT_NOBODY changes method to HEAD and CURLOPT_POST changes it to POST.
Source: http://php.net/manual/en/function.curl-setopt.php
Curl conforms with the force-download header, but file_get_contents can be used to limit the file download download to only 1 byte. This solved the problem!
$postdata = http_build_query(
array(
'username' => "",
'password' => ""
)
);
$params = array(
'http' => array
(
'method' => 'POST',
'header'=>"Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
$ctx = stream_context_create($params);
file_get_contents($url,false,$ctx,0,1);
$size = str_replace("Content-Length: ","",$http_response_header[4]);