Resource id id # 2 by curl php

I am trying to interpret this command line in curl php

curl command:

curl https://api.sandbox.paypal.com/v1/oauth2/token \
 -H "Accept: application/json" \
 -H "Accept-Language: en_US" \
 -u "{clientId}:{secret}" \
 -d "grant_type=client_credentials"

Here is my curl php code:

$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$info = array(
        'grant_type' =>'client_credentials'
);
 $post_field_string = http_build_query($info, '', '&');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                         
    'Accept-Language: en_US')
); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD,'AQHeHhDPIpAoxWaNkXOwCNFV4cZUobhqDa_4qHPKh0iDrSd6rLaxIknT-lmgwd:EJHLmhBMT9dB48kou4V0jyJzaq-CqUlY0zS6QKsxOZKI15hZHZjTfoSV7MO8we');
curl_setopt($ch, CURLOPT_POSTFIELDS,   $post_field_string );
 curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HEADER,1);   
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    

$exec = curl_exec($ch);
curl_exec($ch);

curl_close($ch);

And I get this result:

Resource id #2

I'm new to curl, but I'm trying to learn it. Is this the correct code in my curl php?

I expect this sample response from curl:

{
  "scope": "https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/.*",
  "access_token": "EEwJ6tF9x5WCIZDYzyZGaz6Khbw7raYRIBV_WxVvgmsG",
  "token_type": "Bearer",
  "app_id": "APP-6XR95014BA15863X",
  "expires_in": 28800
}
+4
source share
1 answer

Try this code:

CURLOPT_HTTPHEADER not required if you are passing an array or string.

Added

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); to bypass SSL

<?php
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$info = array(
        'grant_type' =>'client_credentials'
);
 $post_field_string = http_build_query($info, '', '&');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERPWD,'ATKsMxDPf23rhQTgixcTYxLfuJoBsTiIRyaSQW_4J8_rNoVQsXHQkBjmBN0z:EOvF6RBizzf9qH2eA_s3PYmQk--smR6Xe8kDws228lq5pA0IebXTg902FY7f');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_HEADER,1);   
$exec = curl_exec($ch);
echo '<pre>';
print_r($exec);
curl_close($ch);

Answer:

HTTP / 1.1 200 OK Server: Apache-Coyote / 1.1

PROXY_SERVER_INFO: host = slcsbjava3.slc.paypal.com; threadId = 234251 Paypal-Debug-Id: 976e66d30ed12

SERVER_INFO: identitysecuretokenserv: v1.oauth2.token & CalThreadId = 138269 & TopLevelTxnStartTime = 14666662622 & = slcsbidensectoken502.slc.paypal.com & PID = 17346

-ID: 976e66d30ed12

: , 04 2014 10:21:51 GMT

Content-Type: application/json

-: chunked

{
    "scope": "openid",
    "access_token": "A015wXWyeWOj3CprA4dz8uvB.AgGUE-A-p6SuQhw..rmGug",
    "token_type": "Bearer",
    "expires_in": 28800
}
+9

Source: https://habr.com/ru/post/1543175/


All Articles