无法使用cURL进行POST

时间:2015-04-19 09:07:34

标签: php post curl

我尝试使用cURL将某些表单数据发布到网站。它不是一个安全的网站,也不需要登录。它只是一个用来获取某些信息的表单。

表单为here,表单操作位于同一页面(土耳其语中为#)。这就是我尝试用PHP发送POST请求的方式:

$headers = array(
    "content-length: 138",
    "accept-language: en-US,en;q=0.8",
    "accept-encoding: gzip, deflate",
    "referer: http://objektifsonuc.com/",
    "content-type: application/x-www-form-urlencoded",
    "user-agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36",
    "origin: http://objektifsonuc.com",
    "accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "cache-control: max-age=0",
    "connection: keep-alive"
    "host: objektifsonuc.com"
);

$query="il=1&ilce=1%3B18&okul=1%3B18%3B743729%3BTEOGS%2CSBS&sinav=100%3BTEOGS&sinif=8&ogrno=1941&ograd=BERKANT+%DDPEK&ogr=%D6%F0renci+Veli+Giri%FE";

$url = "http://objektifsonuc.com/index.php";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
echo curl_exec($ch);

当前结果:表单页面呈现

期望的结果:包含一些表的其他页面呈现

以下是有效的表单输入:

Şehir: ADANA
İlçe: SARIÇAM
Okul: Hacı Özcan Sinağ Ortaokulu
Sınav Türü: TEOGS | ADANA İL MİLLİ EĞİTİM OKDS
Sınıf: 8
Öğrenci No: 1941
Öğrenci Ad: BERKANT İPEK

这是表格填写的内容:

Filled-out form page

以下是成功发布POST请求时从Chrome获取的表单标题:

POST /index.php HTTP/1.1
Host: objektifsonuc.com
Connection: keep-alive
Content-Length: 138
Cache-Control: max-age=0
Origin: http://objektifsonuc.com
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36
Content-Type: application/x-www-form-urlencoded
DNT: 1
Referer: http://objektifsonuc.com/index.php
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

这是POST正文:

il=1&ilce=1%3B18&okul=1%3B18%3B743729%3BTEOGS%2CSBS&sinav=100%3BTEOGS&sinif=8&ogrno=1941&ograd=BERKANT+%DDPEK&ogr=%D6%F0renci+Veli+Giri%FE

我很难过。是什么导致POST失败?任何人都可以使这个POST成功吗?

1 个答案:

答案 0 :(得分:1)

这应该适合你:

//open connection

$url = 'http://objektifsonuc.com/index.php';

$string = 'il=1&ilce=1%3B18&okul=1%3B18%3B743729%3BTEOGS%2CSBS&sinav=100%3BTEOGS&sinif=8&ogrno=1941&ograd=BERKANT+%DDPEK&ogr=%D6%F0renci+Veli+Giri%FE';

$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($string));
curl_setopt($ch,CURLOPT_POSTFIELDS, $string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);