0

I'm trying to get the data of a page by curl library. I can do it with simple link, but when I'm going to submit a form and then get the data , I can not. It returns me the default value of page, not the submitted form. Let me explain the code.

$url = ...
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
// I'm sending post data
$params = array(
      'madrak'=>5,
      'B1' => 'مشاهده اطلاعات'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// and execute...

The page I'm getting data is written with asp and the form part of that page is this:

<input type="submit" value="مشاهده اطلاعات" name="B1">

<select name="madrak" size="1" class="style1" id="1">
  <option value="1">زير ديپلم</option>
  <option value="5">ديپلم</option>
  <option value="6">فوق ديپلم</option>
  <option value="7">ليسانس</option>
  <option value="8">فوق ليسانس</option>
  <option value="9">دكترا</option>
  <option selected="" value="6">فوق ديپلم</option>
 </select>

It seems that the form is not being submitted. Because just I can get the data which is loading without form submitting. Any idea?

4
  • $info = curl_getinfo($ch); Try to print_r($info); for get result call back. Check HTTP_Code return is 200 or ...? Commented Jul 27, 2016 at 16:16
  • @QuỳnhNguyễn it returns me zero for http_code. But it returns data. Commented Jul 27, 2016 at 17:43
  • I can't believe http_code is 0 and you still got data return. It's impossible Commented Jul 27, 2016 at 17:51
  • @QuỳnhNguyễn Sorry, I was running it before curl_exec(). Yes, it returns 200. Commented Jul 27, 2016 at 18:14

1 Answer 1

1

First I should mention that you don't have to build query string of the post data. You can pass the array.

Please try setting HTTP Headers too. Like content type, user agents and etc, the target website might check for the headers.

But the most important part is to set Content-Type: application/x-www-form-urlencoded HTTP Header.

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
Sign up to request clarification or add additional context in comments.

9 Comments

No, it didn't work. I searched about http_build_query , I found out that I should use this, but without that function, it doesn't work.
@vahidnajafi You are sending the data the right way. Did you try setting other HTTP headers? The best way to test it is to copy all the headers that a browsers sends, then if it still does not give you the right result, you should consider enabled cookies for curl. Many websites use such measures to prevent data crawling.
Whether with or without http_build_query it doesn't work.
What do you mean by copy all the headers that a browsers sends ?
In Google Chrome or Firefox open Developer tools and go to network pane. Resend the data and watch the requests that your browsers sends to the website, click on the main item that is the page, in Headers pane you can see what HTTP headers your browser sends to the server to request that specific page and it contains many. Copy all of them including cookie, test it and if you get the results that you want remove them one by one starting from cookie to make sure what is essential and necessary.
|

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.