PHP curl getting http code 301 - php

I'm using php 5.5.8 and I've got this curl function:
function getBody($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_NOBODY, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT,120);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST,"GET");
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ($curl, CURLOPT_ENCODING, "" );
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1a2pre) Gecko/2008073000 Shredder/3.0a2pre ThunderBrowse/3.2.1.8");
$data = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
return "-1";
}
curl_close($curl);
return $data;
}
These two are supposed to prevent this but i still get 301 error
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
And my safe_mode = off;
I looked for the answer but none of them helped me with this problem
Any suggestions ?
Thank you :)

Related

why my php curl doesn't work when CURLOPT_SSL_VERIFYHOST = 0?

environment:
OS:macOS
PHP:7.1.30
curl:7.65.0
openssl:1.0.2r
I've been trying to call https api with php curl library,it return 403 http code on my machine but 200 on my workmate's Mac.
this is the output html image
my curl configure code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
the configure code above works on my workmate's Mac.
But if I set CURLOPT_SSL_VERIFYHOST = 1 or 2, it works too.
Has anyone been in this situation?

PHP - Curl website behind disclaimer form

I'm trying to parse a website with curl.
However the first screen is a empty form.
So far I'm getting an error (301-302)
Here is my code:
<?php
$url = "https://www.interiorhealth.ca/HedgehogPortal/?returnUrl=/HedgehogPortal/facility/Table";
$url_redirect = "https://www.interiorhealth.ca/HedgehogPortal/facility/Table";
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('FORM_URL', $url);
define('REDIRECT_URL', $url_redirect);
$postValues = array();
//Initiate cURL.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, REDIRECT_URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
$server_output = curl_exec($curl);
//Check for errors!
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
} else {
echo $server_output;
}
?>
Any help or pointers would be gladly appreciated.
Thank you!

PHP Curl - Trying to set Cookies in CURL

Here is my code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: ChosenSite=www"));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: SportsDirect_AnonymousUserCurrency=GBP"));
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: language=en-GB"));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl, CURLOPT_VERBOSE, true);
$str = curl_exec($curl);
curl_close($curl);
I am trying to set these cookies as i am trying to parse HTML dom elements but it seems i am not able to set them correctly.
Can you please tell me where is my mistake, I am setting cookies correctly ?
Thanks in advance!

PHP: Https Curl using Proxy

i'm trying to make a curl request to a https based url. But seems nothing returned back..
I tested with many proxy that worked for non https.
$att = "cookie.txt";
$ip = "121.14.138.56:81";
$curl = curl_init("https://www.att.com");
curl_setopt($curl, CURLOPT_PROXY, $ip);
curl_setopt($curl, CURLOPT_COOKIEJAR, $att);
curl_setopt($curl, CURLOPT_COOKIEFILE, $att);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/27.0.1453.94 Safari/537.36");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$reply = curl_exec($curl);
curl_close($curl);
echo $reply;
Seems this code is working. Note: only some proxy support https..
$proxy = "205.221.221.111:8080";
$curl = curl_init("https://www.att.com");
if (isset($proxy)) {curl_setopt($curl, CURLOPT_PROXY, $proxy);}
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, $att);
curl_setopt($curl, CURLOPT_COOKIEFILE, $att);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/27.0.1453.94 Safari/537.36");
curl_setopt($curl, CURLOPT_COOKIE, 'cookie.txt');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$reply = curl_exec($curl);
curl_close($curl);
var_dump(curl_getinfo($curl));
echo $reply;

php curl function don't save cookie (cookie file : empty)

i have problem that the "PHP" cURL function don't save the cookie !
curl function :
$file['cookie'] = "cooki";
function cURL_Page($url='',$var=''){
global $file;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT,20);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31');
curl_setopt($curl, CURLOPT_REFERER, "http://www.libertyreserve.com/en/login");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if($var) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $var);
}
curl_setopt($curl, CURLOPT_COOKIE,$file['cookie']);
curl_setopt($curl, CURLOPT_COOKIEFILE,$file['cookie']);
curl_setopt($curl, CURLOPT_COOKIEJAR,$file['cookie']);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
in the file "cooki" the are no results
p.s : i curl a https page
thanks all

Categories