MercadoPago Test user - php

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"
}

Related

How can I make this cURL request correct? PHP cURL

I`m using weasyprint by aquavitae for generating PDFs for my Laravel application via cURL
curl -X POST \
-H Content-Type:text/html \
-T ./test.html http://127.0.0.1:5001/pdf?filename=test.pdf \
-o test.pdf
This is an example of cURL request to a weasyprint which gives me a good responce, but when u try to do this in my PHP:
$path_to_file = 'd:/index.html';
$ch = curl_init();
$headers = array(
"Content-Type:text/html"
);
curl_setopt($ch, CURLOPT_URL, 'http://192.168.99.100:5001/pdf');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'datafile' => curl_file_create($path_to_file , mime_content_type($path_to_file), basename($path_to_file))
]
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
curl_close($ch);
I get my responce as a line and when I want to responce it to a browser or into a file I get some information about HTML file I sent:
PDF file I get with PHP cURL
tho my HTML file contains only
<h1>hello world</h1>
I can see something like that when I use Postman too.
I think that there`s some difference beetwen how I send file with CMD cURL and PHP cURL.
Do you guys have any ideas?

Accessing a json api with php

I'm trying to learn how I can interface with a json api.
In the documentation they give me a curl example:
If I run this as a command it works fine, gives me my data in a json format.
I thought I was on the right track with this: PHP + curl, HTTP POST sample code?
but apparently not as I can't figure out what to do with the -H portion of this command.
curl -H "APIKey:My:ApI;key;" -H "Content-Type:.../json" "https://urlofapp.com/API/GetTransaction" -d "{ 'CustomerID':'12345','EndDate':'2018-12-31','StartDate':'2018-01-01'}" > test.json
Trying to get the result into an array that I can sum and show a total of their orders for the year.
From the link I provided above I was trying to start with this:
// set post fields
$post = [
'CustomerID' => 12345,
'StartDate' => 2018-01-01,
'EndDate' => 2018-12-31,
];
$ch = curl_init('https://urlofapp.com/API/GetTransaction');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
The -h command refers the header.
Try below code,
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://urlofapp.com/API/GetTransaction');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{ 'CustomerID':'12345','EndDate':'2018-12-31','StartDate':'2018-01-01'}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Apikey: My:ApI;key;';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I used below to convert the curl command to PHP script,
https://incarnate.github.io/curl-to-php/
Hope it would be useful.
Dealing with curl directly usually ends up being a pain. There are a number of libraries that can help making calls like that much simpler.
Here are a few:
Simple and straighforward: http://requests.ryanmccue.info/
Has everything you could ever possibly want: https://github.com/guzzle/guzzle

Post the data with header in curl not working in php?

I'm trying to create the box app users using PHP. The curl for create user as follows, and it is working on terminal
curl https://api.box.com/2.0/users \
-H "Authorization: Bearer <Access token>" \
-d '{"name": "New User", "is_platform_access_only": true}' \
-X POST
Same thing I have tried with php But it is giving the following error
{"type":"error","status":400,"code":"invalid_request_parameters","help_url":"http:\/\/developers.box.com\/docs\/#errors","message":"Invalid input parameters in request","request_id":"6688622675982fb5339a37"}
The following one I have tried
$developer_token = "TOKEN" ;
$access_token_url = "https://api.box.com/2.0/users";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $access_token_url);
//Adding Parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'name'=>'NEW USER',
'is_platform_access_only'=>'true',
));
//Adding Header
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$developer_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
If I remove the Post parameters, and run with only headers it is give the result of users. But with post it is throws error.
I have rise the same question in Perl tag with Perl code. There I got answer by user #melpomene.
We should encode the data as JSON. It is working,
Then the final code is
$data = array(name=>SOMENAME,is_platform_access_only=>true);
$data = json_encode($data);
$header = array("Authorization: Bearer <TOKEN>");
$ch = curl_init("https://api.box.com/2.0/users/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response1 = curl_exec($ch);
curl_close($ch);

Using cloudflare api to delete a file from cache not working

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

How can I verify that I make a proper POST via cURL in PHP?

I have a sample json object contain these data. I'm trying to store those data in a variable, and passing through curl POST
$json = '{
"mac": "1234567890",
"dns": "8.8.8.8,4.2.2.1",
"acl_mode": 1
}';
$url = 'http://my-site.com/api';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(array('json' => $json)));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
dd($result);
My result is 500
{
"status": 500,
"error_code": 1005
}
Is the way I set up my curl post wrong ?
After testing with curl in the terminal we found out that the endpoint consumes the entire POST payload without a key.
This was used for testing
curl -X POST -d '{"mac": "1234567890","dns": "8.8.8.8,4.2.2.1","acl_mode": 1}' http://mysite/api
So sending the $json payload as it is, without the json_encode and the array encapsulation.
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
worked just fine.

Categories