我希望每次在$fields
循环中创建for
时发送 $registrationIDs = array("a","b","c","D", "E","f","g");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result1 = curl_exec($ch);
curl_close($ch);
$push_devices_limit =2;
if (sizeof($registrationIDs) > $push_devices_limit ) {
for ($i = 0; $i <= sizeof($registrationIDs); $i+= $push_devices_limit) {
$idArr = array_slice($registrationIDs, $i, $i+$push_devices_limit);
$fields = array(
'registration_ids' => $idArr,
'data' => array("message" => $message),
);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
}
,并且每次都不关闭连接时发送到服务器。由于我是PHP的新手,我不知道cURL请求是如何工作的,有人可以用下面的代码帮助我 -
import { Injectable } from '@angular/core';
import { Http, Response, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { AuthInfoInterface } from '../interfaces/authentication';
@Injectable()
export class AuthenticationService {
private url: string = 'api/authentication';
constructor (private http: Http) {}
get(): Observable<AuthInfoInterface> {
let params = new URLSearchParams();
params.set("redirect", "false");
params.set("passive", "true");
return this.http.get(this.url, {search: params})
.map((res: Response) => res.json())
.catch(this.handleError);
}
handleError(error: any) {
let errorMsg = error.message || 'Server Error!';
console.error(errorMsg);
return Observable.throw(errorMsg);
}
}