简体   繁体   中英

POST request with Zend_Http_Client or CURL

I was trying to retrieve data from remote server but it wasn't working & sending empty data. Here is the code that I tried:

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

When I tried to echo the result it's redirecting to index.php page. Any suggestion how to get data. I tried with curl but same problem here also :(

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

and

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

I think this is because of the authentication process in http://www.educationboardresults.gov.bd . When i tried to access the url in the browser it is redirecting to the www.educationboardresults.gov.bd/regular/index.php . You have to use $client->setAuth(); method to login to the site and then get the response body. I haven't used it personally, but Zend_Http_Client - Advanced Usage will give a better idea on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM