Wget or curl download file - Object moved error 302 - php

I prefer if this could be done using wget otherwise update my curl code. I have searched a lot and tried many suggested options like cookies, referer url, user-agent.
Here is my code so far:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.symop.com/config/includes/common/dw.asp?PF=Publications&NF=symop_guide2013.pdf');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_REFERER, "http://www.symop.com/fr/Publications.asp");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
curl_close($ch);
echo $html;
302 Object Moved

Related

Server Error in '/' Application when attempting to cURL

I am trying to download a pdf using cURL and am getting stuck on a "Server Error in '/' Application" page. My code:
$url = "https://some.domain.com/Reports/Report?ReportID=123456"
$ch = curl_init($url);
$header = array ('Host: some.domain.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, implode(':', $arrayCiphers));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_COOKIE, "ASP.NET_SessionId=XXXXXXXX; __RequestVerificationToken_XXXXXXX=lots-of-alpha-numeric-characters");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_REFERER, "https://some.domain.com");
$output = curl_exec($ch);
echo $output;
curl_close($ch);
Is there something else I can try or some more debugging I can do?
[edit] Apparently it's caused by one of my parameters. There are several parameters in the URL. &Flag=True seems to be causing the error. If I change it to &Flag=False I get a blank page.
This error was being caused because my curl request header did not include cookies that were required to download the file. Adding those cookies in the header fixed it.

Site not loading using curl

<?php
$url='https://source.amazon.com/forcelogin?returnToURL=https://www.amazon.com/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT,
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
curl_setopt($ch, CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
$data = curl_exec($ch);
curl_close($ch);
?>
I don't know why it doesn't load the website.
If I put regular sites like google.com, amazon.com or anything else is working.Anyone can help me out?
You need to add CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST options to access HTTPS URL.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

PHP/cURL how to get Request Cookies

Hy,
I have a bit of a problem here. One client of mine asked to create a login script for a website built in ASP. As much as I can take and use Response Cookies, I am not able to take and use Request Cookies. I tried, but they are not saved in my cookie files. As such, I cannot login into the very website. Its address is http://www.itraceuk.co.uk/Default.asp?secure=signin
Here is the code I am currently using:
$ch4 = curl_init();
curl_setopt ($ch4, CURLOPT_URL, $posturl);
curl_setopt($ch4, CURLOPT_HEADER, true);
curl_setopt ($ch4, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch4, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch4, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36");
curl_setopt ($ch4, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch4, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch4, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch4, CURLOPT_COOKIESESSION, 1);
curl_setopt ($ch4, CURLOPT_COOKIEJAR, $cookie2);
curl_setopt ($ch4, CURLOPT_COOKIEFILE, $cookie2);
curl_setopt ($ch4, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin');
$result4 = curl_exec ($ch4);
curl_close($ch4);
<?php
$posturl = 'http://www.itraceuk.co.uk/Default.asp?secure=signin';
$cookie = __DIR__ . '/cookie.txt'; // file and folder must be writeable
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.itraceuk.co.uk/Default.asp?secure=signin');
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
var_dump(file_get_contents($cookie));
Set cookie folder/file permissions to writeable by PHP and Server.
Reuse the cookie data after the login.
Curl Post to the login form with correct credentials, that logs you in and sets the cookie (curl CURLOPT_POSTFIELDS). Then reuse this cookie for the second curl request (this code here).
For a complete example of a HTTPS cURL login, please see: https://stackoverflow.com/a/10307956/1163786

Unable to login Google with curl

I want to login in google.com with php via curl but i am getting login page again and again.
Here is my code
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.79 Safari/537.1 AlexaToolbar/alxg-3.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
var_dump($result);
Also it is not creating the cookie file
I found this link Login to Google with PHP and Curl, Cookie turned off? . there is also the same code as mine but also i am unable to login.
What is wrong in this code ?

Using cURL to download a site's HTML source, but getting different file than intended

I'm trying to use cURL and PHP to download the HTML source (as it appears in the browser) of here. But instead of the actual source code, this is returned instead (a meta refresh link set to 0).
<html>
<head><title>Object moved</title></head>
<body>
<h2>Object moved to here.
</h2>
</body>
</html>
I'm trying to spoof the referral header to be the site, but it seems I'm doing it wrong. Code is below. Any suggestions? Thanks
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.windowsphone.com/en-US/apps/ea39f002-ac30-e011-854c-00237de2db9e');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://www.windowsphone.com/en-US/apps/ea39f002-ac30-e011-854c-00237de2db9e");
$html = curl_exec($ch);
curl_close($ch);
Add the curl option to follow redirects:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
If it is a meta refresh and not an HTTP moved header, see:
PHP: Can CURL follow meta redirects
As mentioned by flesk, you may also need to store the cookies.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.windowsphone.com/en-US/apps/ea39f002-ac30-e011-854c-00237de2db9e');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.6 (KHTML, like Gecko) Chrome/16.0.897.0 Safari/535.6');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_REFERER, "http://www.windowsphone.com");
$html = curl_exec($ch);
curl_close($ch);
echo $html;
The problem isn't the referrer but that you need to enable cookies for it to work.
Try something like this:
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
You have to query the page twice. First allow redirects to get the cookie from login.live.com, then query again with the cookie set.

Categories