cURL POST请求PHP中的API不返回任何值

时间:2012-02-29 03:27:49

标签: php ruby api curl microsoft-translator

我正在尝试获取Microsoft的Translator API的访问令牌,这一直非常困难。我首先尝试使用Ruby和HTTParty编写请求,但它不接受我的参数。 Microsoft在PHP中提供了一个示例,所以我想我可以使用它:

http://msdn.microsoft.com/en-us/library/hh454950.aspx#phpexample

代码是直接从Microsoft网站复制的 - 我只是添加了我的变量并在最后调用了函数:

<?php

/*
 * Get the access token.
 *
 * @param string $grantType    Grant type.
 * @param string $scopeUrl     Application Scope URL.
 * @param string $clientID     Application client ID.
 * @param string $clientSecret Application client ID.
 * @param string $authUrl      Oauth Url.
 *
 * @return string.
 */
function getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl){
    try {
        //Initialize the Curl Session.
        $ch = curl_init();
        //Create the request Array.
        $paramArr = array (
             'grant_type'    => $grantType,
             'scope'         => $scopeUrl,
             'client_id'     => $clientID,
             'client_secret' => $clientSecret
        );
        //Create an Http Query.//
        $paramArr = http_build_query($paramArr);
        //Set the Curl URL.
        curl_setopt($ch, CURLOPT_URL, $authUrl);
        //Set HTTP POST Request.
        curl_setopt($ch, CURLOPT_POST, TRUE);
        //Set data to POST in HTTP "POST" Operation.
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr);
        //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec().
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate.
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //Execute the  cURL session.
        $strResponse = curl_exec($ch);
        //Get the Error Code returned by Curl.
        $curlErrno = curl_errno($ch);
        if($curlErrno){
            $curlError = curl_error($ch);
            throw new Exception($curlError);
        }
        //Close the Curl Session.
        curl_close($ch);
        //Decode the returned JSON string.
        $objResponse = json_decode($strResponse);
        if ($objResponse->error){
            throw new Exception($objResponse->error_description);
        }
        $finalresponse = $objResponse->access_token;
        return $finalresponse;

    } catch (Exception $e) {
        echo "Exception-".$e->getMessage();
    }

}

    $grant = "client_credentials"
$scope = "http://api.microsofttranslator.com";
$secret = "8Bo0SET1W8lmDgfV/l7FLEKUWRDXCEZrvTgv9tSH3Bs=";
    $client = "globalchatapp"
$auth = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";


getTokens($grant, $scope, $client, $secret, $auth);


?>

无论如何,当我运行它(使用PHP 5.3.6)时,没有错误;它没有返回任何价值。我对PHP并不擅长,它可能是一个明显的东西,我只是没有看到,但我已经挣扎了一段时间无济于事。任何帮助将不胜感激。

此外,如果这有帮助,这是我原来的Ruby代码。我确实收到了服务器的响应,说“范围”丢失了:

require 'httparty'
scope = "http://api.microsofttranslator.com";
secret = "8Bo0SET1W8lmDgfV/l7FLEKUWRDXCEZrvTgv9tSH3Bs=";
auth = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
client_id = "globalchatapp"
grant_type = "client_credentials"

response = HTTParty.post("https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/?&scope=#{scope}&secret=#{secret}&client_id=#{client_id}&grant_type=#{grant_type}", :body => {:key => :value})
puts response

再次感谢!

1 个答案:

答案 0 :(得分:0)

getTokens()返回的值存储在变量中,并将其传递给urldecode()以解码%2F和其他编码字符。

$tokens = getTokens($grant, $scope, $client, $secret, $auth);
echo urldecode($tokens);