我有一個PBX服務器(Asterisk 1.8),並且我可以通過請求中的某些GET變量管理調用。我的問題是我需要先登錄到服務器。一旦我這樣做了,我想要保存一個cookie,然後將其一起發送以進行驗證以及我的下一個要求。通過HttpRequest在PHP中發送cookies以及請求
我有一個當前工作使用捲曲代碼:
public function authenticate(){
$temp_dir = sys_get_temp_dir();
$ckfile = tempnam($temp_dir, "ast");
$auth_url = $this->ast_link."?action=login&username=asterisk_http&secret=*****";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$auth_response = curl_exec($ch);
curl_close($ch);
$this->auth_cookie = $ckfile;
return $ckfile;
}
//$call contains id, patcode, phone_number, call_type
public function makeCall($overload, $call, $exten, $user = NULL){
set_time_limit(0);
if(!$this->auth_cookie) $this->authenticate();
//http://blah.server/asterisk/rawman?action=originate&channel=$somechannel
$call_url = $this->generateCallUrl($overload, $call, $exten, $user);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $call_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->auth_cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
不幸的是捲曲等待來自服務器,只發生在一個呼叫結束的響應。因此,我無法同時撥打多個電話。
我該如何在PHP中使用HttpRequest以與cURL相同的方式發送cookie以及請求?我對這個背後的網絡機制有點不熟悉,所以請原諒我對此的無知。
TL; DR:如何隨着HttpRequest的
在'HttpRequest'你可以使用'enableCookies'方法來接收和發送的cookie,但是這將不會工作,如果你要保存的cookies並在另一個腳本調用中使用它們。 – user1950929