Can someone please help me convert this to PHP code:
curl -v https://api.sandbox.paypal.com/v1/payments/payment/PAY-98656197DP057202KKO5NMSY/execute/ \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer A015-m7XgI9uXtd0g4sckNFH3bEaxrAiFlbTecVe9SSgoX4' \
-d '{ "payer_id" : "XMS2L726YHQQY" }'
This is what I have so far:
$post = json_encode(array('payer_id' => Session::get('paypal_PayerID')));
$curl = curl_init('https://api.sandbox.paypal.com/v1/payments/payment/'.Session::get('paypal_id').'/execute/');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: '.Session::get('paypal_token_type').' '.Session::get('paypal_access_token')));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$html = curl_exec($curl);
$status = curl_getinfo($curl);
curl_close($curl);
I have been at it for hours but paypal is not giving me the same reponses, (e.g. it worked two times, but most of the time it returns a 500 error).
Maybe I'm just really loosing it here as it is 2AM in the morning...
And before anyone asks, Yes, I have verified that those session variables have values before sending the curl request.
I believe the order of your curl_setopt calls matters behind the scenes. Try placing your CURLOPT_HTTPHEADER call last. You might also consider using Fiddler or something similar to sniff your HTTP requests to easily determine if there are any effectual differences.
Related
I have to make a curl request in php with these parameters, can you help me with an example, since I can't find anything similar
the link curl
curl -X GET "https://example.com/nms/api/v2.1/devices?withInterfaces=true&authorized=false&type=uisps&role=switch" -H "accept: application/json" -H "x-auth-token: wsedr4455-3345-es45-2345-4edeesssd"
You should transform it into PHP curl syntax. It should work like this:
$ch = curl_init('https://example.com/nms/api/v2.1/devices?withInterfaces=true&authorized=false&type=uisps&role=switch');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'accept: application/json',
'x-auth-token: wsedr4455-3345-es45-2345-4edeesssd'
));
$result = curl_exec($ch);
curl_close($ch);
The JSON incoming is in $result.
I have a curl request that was given to me as part of an API documentation, and can't for the life of me get it to work via php (in Laravel).
The documentation says to do this...
wget -O- \
--header 'x-user:x' \
--header 'x-password:y' \
'http://example.com'
What I'm trying is this...
$url = 'http://example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "test_user:test_password");
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
dd($info);
This is just echoing out Authentication Failed and then a bunch of info on my request, but not actual data.
Can anyone point out what I'm doing wrong?
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'x-user: something', 'x-password: something' ));
Here is my code for trying to delete a file a file via the api
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY-ZONE-ID>/purge_cache");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = [
'X-Auth-Email: MYEMAIL',
'X-Auth-Key: MY-AUTH-KEY',
'Content-Type: application/json'
];
$data = json_encode(array("files" => "https://example.com/file/".$filename));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
The response i get is as follows
{
"success":false,
"errors":[
{
"code":1012,
"message":"Request must contain one of \"purge_everything\" or \"files\", or \"tags"
}
],
"messages":[
],
"result":null
}
Documentation says tag is optional so it should work
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
-H "X-Auth-Email: user#example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"files":["http://www.example.com/css/styles.css"],"tags":["some-tag","another-tag"]}'
What am In doing wrong here ?
I maybe because your files property is not an array.
Try
$data = json_encode(array("files" => array("https://example.com/file/".$filename)));
This is a bit confusing. Post Data is usually sent in key value pairs.
Also when the post data is an array curl changes the content type to multipart/form-data
When post data is sent as a query string, it's in the format of key1=value1&key2=value2
It appears the json is the value with no key.
I would try it both as an array and string and look at the request header.
In the request header look at the content-type and the data.
To get the request header in the curl return add this option:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
You may need to add:
curl_setopt($ch, CURLOPT_POST, true);
$data[] = json_encode(array('files'=>"https://example.com/file/$filename"));
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$data = json_encode(array('files'=>"https://example.com/file/$filename"));
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
PS: You do not need to use the . concatenation when using double quotes for $filename
I'm struggling to create some test users for the button.
Following these instructions
Create test users
This code is supposed to be used to get the tests users, but i have no idea where to use that, or how, and the documentation is kinda poor
Request - Curl
curl -X POST \
-H "Content-Type: application/json" \
'https://api.mercadolibre.com/users/test_user?access_token=your_access_token' \
-d '{"site_id":"MLA"}'
The website is made in PHP
Any bit of advice?
This is curl used from the command line you can change this to a php file assuming that curl is enabled.
<?php
// add your access token
$access_token = "your_access_token";
$data = array('site_id' => 'MLA');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL,"https://api.mercadolibre.com/users/test_user?access_token=$access_token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
$response = curl_exec($ch);
// json response
echo $response;
you can convert the json $response to an array using json_decode() and process the data
sample output
{
"id": 123456,
"nickname": "TT123456",
"password": "qatest123456",
"site_status": "active",
"email": "test#test.com"
}
I'm trying to translate the following cURL command to PHP.
curl -k https://api.box.com/2.0/files/content -H "Authorization: Bearer 8tuuIMsrf3ShyODVdw" -F filename=#txt.txt -F folder_id=0
Here is my attempt:
$url = 'https://api.box.com/2.0/folders/content';
$params['filename'] = '#txt.txt';
$params['folder_id'] = '0';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer ".$this->access_token, "Content-Type: multipart/form"));
$data = curl_exec($ch);
curl_close($ch);
It's not working however. Can anyone help me understand why what I'm doing is wrong and what I can do to modify my code? Thanks!!
Try getting rid of the header "Content-Type: multipart/form" from your CURLOPT_HTTPHEADER option as cURL will take care of this for you based on your post data.
Since you are uploading a file, the content-type will automatically be set to multipart/form-data which is actually the correct content type, not multipart/form.
curl -k means insecure. Add the following line:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
Translated from the german manual (Note that the english version is missing this information):
If CURLOPT_SSL_VERIFYPEER has been deactivated, the option CURLOPT_SSL_VERIFYHOST has to be disabled as well.
The rest of your code should work fine.