簡體   English   中英

cURL POST對PHP中的API的請求未返回任何值

[英]cURL POST Request to API in PHP not returning any values

我正在嘗試獲取Microsoft的Translator API的訪問令牌,這已經很艱難了。 我首先嘗試使用Ruby和HTTParty編寫請求,但是它不接受我的參數。 Microsoft用PHP提供了一個示例,所以我認為我可以使用它:

http://msdn.microsoft.com/zh-CN/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

再次感謝!

getTokens()返回的值存儲在變量中,並將其傳遞給urldecode()以對%2F和其他編碼的字符進行解碼。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM