I have the following code from PayPal Payout SDK to get access tokens from the PayPal API.
curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "CLIENT_ID:SECRET" \
-d "grant_type=client_credentials"
To get the access token, I have tried the following.
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = ['grant_type:client_credentials'];
$response = Http::withHeaders([
'Accept:application/json',
'Accept-Language:en_US',
"Content-Type: application/x-www-form-urlencoded"
])->withBasicAuth($client_id, $secret)
->post($url, $data);
// OR
$response = $client->request('POST', $url, [
'headers' => [
'Accept' => 'application/json',
'Accept-Language' => 'en_US',
'Authorization ' => ' Basic ' .
base64_encode($client_id . ':' . $secret)
],
'form_params' => [
'grant_type' => 'client_credentials',
]
]);
laravel 7 or 8 solution :
$client_id = "AWN5555";
$secret = "44444";
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
$data = [
'grant_type' => 'client_credentials',
];
$response = Http::asForm()
->withBasicAuth($client_id, $secret)
->post($url, $data);
php native solution :
$client_id = "AWN5555";
$secret = "44444";
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api-m.sandbox.paypal.com/v1/oauth2/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
CURLOPT_HTTPHEADER => [
'Authorization: Basic '.base64_encode($client_id.':'.$secret)
],
]);
$response = curl_exec($curl);
curl_close($curl);
Related
I need below code to php curl request to remote server. i am not able to do this and not getting source from google
curl -X PATCH
--header "Content-Type: application/json"
--header "Accept: application/json"
--header "x-api-token: API_TOKEN"
--header "x-api-user: API_USER" --data '{"profile_id":"string","msg_product":["A2P","P2P"]}'
"https://api.telnyx.com/messaging/numbers/{tn}"
Some php equivalent could be :
using curl :
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.telnyx.com/messaging/numbers/'.$tn,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS =>'{"profile_id":"string","msg_product":["A2P","P2P"]}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Accept: application/json',
'x-api-token: API_TOKEN',
'x-api-user: API_USER'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
or using guzzle :
<?php
$client = new Client();
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'x-api-token' => 'API_TOKEN',
'x-api-user' => 'API_USER'
];
$body = '{
"profile_id": "string",
"msg_product": [
"A2P",
"P2P"
]
}';
$request = new Request('PATCH', 'https://api.telnyx.com/messaging/numbers/'.$tn, $headers, $body);
$res = $client->sendAsync($request)->wait();
echo $res->getBody();
I'm not able to understand how to modify this cURL into Laravel 5.8, getting {"code":"11", "message":"invalid Request found", "status":"DECLINED"} response.
Here is my cURL code which is working fine (in postman and browser):
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.hylo.biz/Api/v1.0/Payment',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"orderId":"KJDHKSGIU768",
"amount":"100",
"redirect_url":"google.com"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Here is my laravel code:
public function process(Request $request)
{
// return $request['request'];
$client = new \GuzzleHttp\Client();
$url = "https://api.hylo.biz/Api/v1.0/Payment";
$response = $client->request('POST', $url, [
'headers' => [
'Authorization' => 'Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type' => 'application/json'
],
'form_params' => $request['request']
]);
return $response = $response->getBody();
}
in this way it's working
public function process(Request $request)
{
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.hylo.biz/Api/v1.0/Payment',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"orderId":"KJDHKSGIU768",
"amount":"100",
"redirect_url":"google.com"
}',
CURLOPT_HTTPHEADER => [
'Authorization: Basic WUR1pYQ1hYY3U2Og2OGM6JJE5tWEDJhJDEwZReVE=',
'Content-Type: application/json',
],
]);
$response = curl_exec($curl);
curl_close($curl);
}
and also I forgot to add use Illuminate\Http\Request;
I'm trying to make a POST request and send some values in the body of an API call. In the documentation of the API it says I need to make a POST request, using startUrls as an array with key and value.
<?php
$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID';
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/json',
'body' => json_encode($postData)
)
));
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
?>
The JSON seems to be how it should, but the script is not sending the body properly to the website.
According to the documentation, there is no body option for the HTTP context. Try content instead:
<?php
$url = "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID";
$postData = [
"startUrls" => [
["key"=>"START", "value" => "https://instagram.com/instagram"]
]
];
$context = stream_context_create([
"http" => [
"method" => "POST",
"header" => "Content-type: application/json",
"content" => json_encode($postData)
]
]);
$resp = file_get_contents($url, FALSE, $context);
print_r($resp);
The following code will work. I have set the headers and specified the content type.
$request = new HttpRequest();
$request->setUrl('$url = 'https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID');
$request->setMethod(HTTP_METH_POST);
$request->setHeaders(array(
'cache-control' => 'no-cache',
'content-type' => 'application/x-www-form-urlencoded'
));
$request->setContentType('application/x-www-form-urlencoded');
$request->setPostFields(array('key'=>'START', 'value'=>'https://instagram.com/instagram')
));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
If you want to try with cUrl the following code snippet will work.
$curl = curl_init();
$postData = array(
'startUrls' => array(array('key'=>'START', 'value'=>'https://instagram.com/instagram'))
);
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.apify.com/v1/USERID/crawlers/CRAWLERID/execute?token=TOKENID",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"content-type: multipart/form-data;"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
When trying to revoke the oauth access_token for Box, I get the error : Client id was not found in the headers or body
This is the curl-command (which works fine) :
curl https://api.box.com/oauth2/revoke -d 'client_id=CLIENT_ID&client_secret=CLIENT_SECRET&token=access_token' -X POST
When trying the same with php curl, I get the error.
<?php
$revokeurl="https://api.box.com/oauth2/revoke";
$dataq = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'token' => $access_token
);
$dataqjson= json_encode($dataq);
$headers=array(
"Content-Type: application/json"
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $revokeurl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $dataqjson,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
));
$response = curl_exec($curl);
$json_response = json_decode( $response, TRUE );
curl_close($curl);
?>
Why is my POST with json body not correct ?
Data should be set as application/x-www-form-urlencoded rather than JSON-encoded. Example:
<?php
$revokeurl = "https://api.box.com/oauth2/revoke";
$dataq = array(
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'token' => 'access_token',
);
$curl = curl_init();
curl_setopt_array(
$curl,
array(
CURLOPT_URL => $revokeurl,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($dataq),
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
)
);
$response = curl_exec($curl);
$json_response = json_decode($response, false);
print_r($json_response);
curl_close($curl);
Im' looking to consume a Rest API using Zend Framework 2.
I tried :
How to consume a Rest API using Zend Framework 2
$request = new Request();
$request->getHeaders()->addHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8'
));
$request->setUri($url);
$request->setMethod('POST');
$request->setPost(new Parameters(array('param1' => 'val1')));
$client = new Client($url, array(
'sslverifypeer' => null,
'sslallowselfsigned' => null,
));
$response = $client->dispatch($request);
$data = json_decode($response->getBody(), true);
Can someone provide an example how to enter username and password :
I tried this :
$request->getHeaders()->addHeaders(array(
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
'user' => 'username1',
'password' => 'password1'
));
From https://framework.zend.com/manual/2.4/en/modules/zend.http.client.advanced.html#http-authentication
$client->setAuth('username1', 'password1');
it works now, but with curl :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "param1=value1",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"password: password01",
"user: user01"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}