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' ));
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'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);
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"
}
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.
Teaching myself some Facebook feed posting via PHP and I'm trying to figure out how I would execute this curl HTTP POST via the the PHP curl command, any suggestions?
curl -X POST \
-F 'message=Post%20with%20app%20access%20token' \
-F 'access_token=YOUR_APP_ACCESS_TOKEN' \
https://graph.facebook.com/4804827/feed
It's found here
This is how to do it:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/4804827/feed");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'message' => 'Post with app access token',
'access_token' => 'YOUR_APP_ACCESS_TOKEN'
));
echo curl_exec($ch);
curl_close($ch);
I recommend looking at the documentation:
http://www.php.net/manual/en/function.curl-init.php
http://www.php.net/manual/en/function.curl-setopt.php