I have rather strange issue. I have an access to site(email and password). Sorry, but I can not show this site to you. I need to get some info from it's content. Instead of that I gonna show you my code.
//I form string of post request
$fields=array(
'name_of_login_field' => urlencode('[email protected]'),
'name_of_password_field' => urlencode('pass')
);
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
$fields_string=rtrim($fields_string, '&');
Then I send it via curl.
$curlURL="URL";
if( $curl = curl_init() )
{
curl_setopt($curl, CURLOPT_URL, $curlURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_USERAGENT, "mozilla/5.0 (ipad; cpu os 7_0_4 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11b554a safari/9537.53");
$out = curl_exec($curl);
var_dump($out);
//var_dump($out);
curl_close($curl);
}
A site has a form with two inputs (login-password). Also it has a submit button with name and some hidden input with name - redirect.
If I set an google.com, or our site as $curCURL I receive a string of its content. If I use URL of site I need to parse, I receive empty string. How it is possible. I ask for suggestions. May be someone met anything similar?
UPDATE
Here is my fresh curl:
if( $curl = curl_init() )
{
curl_setopt($curl, CURLOPT_URL, $curlURL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, "mozilla/5.0 (ipad; cpu os 7_0_4 like mac os x) applewebkit/537.51.1 (khtml, like gecko) version/7.0 mobile/11b554a safari/9537.53");
$out = curl_exec($curl);
var_dump($out);
//var_dump($out);
curl_close($curl);
}
With that curl $out is false. If I remove string with CURLOPT_FOLLOWLOCATION $out is empty string.
UPDATE1
I checked
curl -l desired-site.com
it returned only content without headers. Then I checked
curl -s -D - desired-site.com -o /dev/null
It returned this headers.
HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Thu, 07 May 2015 08:20:23 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.4-14+deb7u11
Set-Cookie: PHPSESSID=randon_number_of_letters; expires=Sat, 09-May-2015 12:07:03 GMT; path=/
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache
Pragma: no-cache
Cache-Control: private
Cache-Control: no-store, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Last-Modified: Thu, 01 Jan 1970 00:00:01 GMT
Can it help somehow?
UPDATE2 CURLOPT_VERBOSE gives same results. But when I set CURLOPT_HEADER to true, I can see headers (without follow location, with it it still returns false)
UPDATE3
I do such things to set cookie:
preg_match('/PHPSESSID=([A-Za-z0-9]+)/',$out, $matches);
$cookie="Cookie: PHPSESSID=".$matches[1];
And then add to curl:
curl_setopt($curl1, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl1, CURLOPT_COOKIEJAR, $cookie);
I've made another connection: $curl1=curl_init()
And I do var_dump of $cookie and headers of responce. The PHPSESSID is different there. I should do, what @baf have said in comments in some other way?
(you would have to open the form page, store cookies and then post to it again with the cookies)
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);CURLOPT_FOLLOWLOCATION)?