4

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)

16
  • Seeing as how this is to do with the different between 3 urls (Google, yours and the desired), its going to be difficult to figure out the error without knowing the url/site involved. Is it a valid domain name? Are you getting connection refused? Sorry if I've misunderstood your question. Commented May 7, 2015 at 7:56
  • A shot in the dark, but try using the following curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); Commented May 7, 2015 at 7:59
  • Maybe you should follow a redirect (CURLOPT_FOLLOWLOCATION)? Commented May 7, 2015 at 8:01
  • Well, as far as I don't receive connection refuse, just empty string. It should return something, for example mistake with authentication, or anything like that. Commented May 7, 2015 at 8:02
  • 2 advices with FOLLOWLOCATION, sure I'll try it Commented May 7, 2015 at 8:02

2 Answers 2

1

This is what I suggested in the discussion in comments. It stores cookies in a file and makes two requests. One just opens form page and saves the cookies in a cookie jar. Second request posts the data. You may want to give it a try.

$jar = tempnam('/tmp', 'cookie'); // create temporary file

$curl = curl_init($url_1); // open form page
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_COOKIEJAR, $jar);
curl_exec($curl);
curl_close($curl);

$curl = curl_init($url_2); // post data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_HEADER, false); 
curl_setopt($curl, CURLOPT_COOKIEFILE, $jar);
$out = curl_exec($curl);
curl_close($curl);

unlink($jar); // delete temporary file
Sign up to request clarification or add additional context in comments.

3 Comments

Thats right. Discussion in comments and exactly you helped me a log. So I've approved your answer. But as you can see I do it in one request (exec) and don't use cookiejar/
@sergey Great that you found a solution. Though I am a bit surprised that setting fake session id (900000jjsakjas) works :)
helped me a lot*, (typo=)) Sure it doesn't work with fake session id, I used it for example.
0

Finally, I've done this. I got PHPSESSID from chrome debugger cookie tab. Then I did folowing (just simply send cookie to URL):

    if( $curl = curl_init() ) {
    curl_setopt($curl, CURLOPT_URL, $curlURL);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);

    curl_setopt($curl, CURLOPT_COOKIE, "PHPSESSID=900000jjsakjas");
    $out = curl_exec($curl);
    echo $out;

    curl_close($curl);
  }

This returns fine result.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.