[英]POST request with Zend_Http_Client or CURL
我試圖從遠程服務器檢索數據,但是它不起作用並發送空數據。 這是我嘗試的代碼:
define('WWW_PATH', dirname(__FILE__) . '/../..');
set_include_path(implode(PATH_SEPARATOR, array(realpath(WWW_PATH . '/library'), get_include_path())));
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Zend_');
$data = array(
'sr' => '3',
'et' => '3',
'exam' => 'ssc',
'year' => '2007',
'board' => 'dhaka',
'roll' => '494548'
);
$client = new Zend_Http_Client();
$client->setUri('http://www.educationboardresults.gov.bd/regular/result.php');
$client->setParameterPost($data);
$response = $client->request('POST');
$result = array('data'=>$response->getBody());
echo json_encode($result);
當我嘗試回顯結果時,它正在重定向到index.php頁面。 任何有關如何獲取數據的建議。 我嘗試了卷曲但是這里也有同樣的問題:(
$post_data['sr'] = '3';
$post_data['et'] = '3';
$post_data['exam'] = 'ssc';
$post_data['year'] = '2007';
$post_data['board'] = 'dhaka';
$post_data['roll'] = '494548';
foreach ( $post_data as $key => $value)
{
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
$curl_connection = curl_init('http://www.educationboardresults.gov.bd/regular/result.php');
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_POST, 1);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_connection);
curl_close($curl_connection);
//var_dump($result);
$data= array('data'=>$result);
echo json_encode($data);
和
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => "http://www.educationboardresults.gov.bd/regular/result.php",
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => array(
'sr' => '3',
'et' => '3',
'exam' => 'ssc',
'year' => '2007',
'board' => 'dhaka',
'roll' => '494548',
),
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
$data= array('data'=>$result);
echo json_encode($data);
我認為這是由於http://www.educationboardresults.gov.bd
中的身份驗證過程http://www.educationboardresults.gov.bd
。 當我嘗試在瀏覽器中訪問URL時,它正在重定向到www.educationboardresults.gov.bd/regular/index.php
。 您必須使用$client->setAuth();
登錄網站然后獲取響應正文的方法。 我還沒有親自使用過它,但是Zend_Http_Client-Advanced Usage將為您提供一個更好的主意。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.