使用上下文卷曲而不是file_get_contents

时间:2017-04-07 14:14:38

标签: php curl file-get-contents

我想用curl(sipgate voip帐户)替换file_get_contents,但每次试用都不起作用。

版本mit file_get_contents:

    $requestParameter = array(
    'RemoteUri' => sprintf('sip:%s@sipgate.de', $remoteUri),
    'LocalUri' => sprintf('sip:%s@sipgate.de', $localUri),
    'TOS' => 'voice'
);
    $auth = base64_encode(sprintf('%s:%s', $username, $password));
    $request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
    $context = stream_context_create(
    array('http' => array(
        'method' => "POST",
        'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
        'content' => $request
    ))
);
    file_get_contents("https://api.sipgate.net/RPC2", false, $context);

curl的最新试用

    $requestParameter = array(
    'RemoteUri' => sprintf('sip:%s@sipgate.de', $remoteUri),
    'LocalUri' => sprintf('sip:%s@sipgate.de', $localUri),
    'TOS' => 'voice'
);
    $auth = base64_encode(sprintf('%s:%s', $username, $password));
    $request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
    $context = stream_context_create(
    array('http' => array(
        'method' => "POST",
        'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
        'content' => $request
    ))
);
    $url = 'https://api.sipgate.net/RPC2';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $context);
    curl_exec($curl);
    curl_close($curl);

    $requestParameter = array(
    'RemoteUri' => sprintf('sip:%s@sipgate.de', $remoteUri),
    'LocalUri' => sprintf('sip:%s@sipgate.de', $localUri),
    'TOS' => 'voice'
);
    $auth = base64_encode(sprintf('%s:%s', $username, $password));
    $request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);

    $url = 'https://api.sipgate.net/RPC2';
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, $auth);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
    curl_exec($curl);
    curl_close($curl);

有人对我有想法吗? Thx,spf

1 个答案:

答案 0 :(得分:0)

两个版本的上述命名错误

$requestParameter = array(
'RemoteUri' => sprintf('sip:%s@sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s@sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
    'method' => "POST",
    'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
    'content' => $request
    ))
);
$data = http_build_query($context);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);

$requestParameter = array(
'RemoteUri' => sprintf('sip:%s@sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s@sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);

$data = http_build_query($request);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);