2017-11-29 11 views
0

Vor allem

  • I Yelp API mit PHP verwenden möchten.
  • Ich möchte Bearer Access Token erhalten, kann aber nicht.

-Code

Der folgende Code ist eine, die ich ausgeführt.[API] Mit Yelp API/PHP

$url = "https://api.yelp.com/oauth2/token"; 
$clientEncode = urlencode("?grant_type=client_credentials?client_id=MY_CLIENT_ID?client_secret=MY_CLIENT_SECRET"); 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientEncode); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded")); 
$response = curl_exec($curl); 
$array = json_decode($response, true); 
$arrayVal = array_values($array); 
$bearer_token = $array->access_token; 
echo '<pre>'; 
var_dump($array); 
echo '</pre>'; 

Und die folgende Nachricht ist die Fehlermeldung.

/home/ubuntu/workspace/index.php:21: 
class stdClass#1 (1) { 
public $error => 
class stdClass#2 (2) { 
public $code => 
string(16) "VALIDATION_ERROR" 
public $description => 
string(165) "client_id or client_secret parameters not found. Make sure to provide client_id and client_secret in the body with the application/x-www-form-urlencoded content-type" 
} 
} 

Vielleicht ist die Verwendung von Curl, ist Okay, denke ich, aber ich weiß nicht, wie die Parameter in der URL hinzuzufügen.

Bitte unterrichten Sie mich mehr gute Praxis.

Danke.

Antwort

0

Hoffe, dass ich diese für Sie arbeiten wird :)

<? 
    $clientEncode = urlencode("https://api.yelp.com/oauth2/token?client_id=MY_CLIENT_ID&client_secret=MY_CLIENT_SECRET"); 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $clientEncode); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded")); 
    $response = curl_exec($curl); 
    $array = json_decode($response, true); 
    $arrayVal = array_values($array); 
    $bearer_token = $array->access_token; 
    echo '<pre>'; 
    var_dump($array); 
    echo '</pre>'; 

    ?> 
+1

Sie für Ihre Beantwortung danken. Ich kann nicht über "grant_type" Beitrag in POSTFIELDS denken. Aber ich habe dieses Problem später selbst auf andere Weise gelöst. –