使用Zend_Http_Client或CURL发出POST请求

时间:2013-11-10 16:19:14

标签: php zend-framework curl zend-http-client

我试图从远程服务器检索数据,但它无法正常工作&发送空数据。这是我试过的代码:

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);

1 个答案:

答案 0 :(得分:0)

我认为这是因为http://www.educationboardresults.gov.bd中的身份验证过程。当我尝试访问浏览器中的网址时,它会重定向到www.educationboardresults.gov.bd/regular/index.php。您必须使用$client->setAuth();方法登录该站点,然后获取响应正文。我没有亲自使用它,但Zend_Http_Client - Advanced Usage会对此有更好的了解。