Replace curl with file_get_content - php

I need to replace curl with file_get_content ( I have removed sensitive informations ):
curl -F 'client_id=aaa' \
-F 'client_secret=bbb' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=http://testtest.altervista.org/instagram/' \
-F 'code=ccc' \
https://api.instagram.com/oauth/access_token
I tryed this but doens't work ( it return nothing ):
$url = 'https://api.instagram.com/oauth/access_token?client_id=aaa&client_secret=bbb&grant_type=authorization_code&redirect_uri=http://testtest.altervista.org/instagram/&code=ccc';
print json_decode(file_get_contents($url));
Why ?

Not sure why you want to change cURL to a file_get_contents, but I would at least make sure to encode the querystring you're trying to use. You could use http_build_query to get proper results
$query = array(
'client_id' => 'aaa',
'client_secret' => 'bbb',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://testtest.altervista.org/instagram/',
'code' => 'ccc
);
$domain = 'https://api.instagram.com/oauth/access_token?'
$url = $domain . http_build_query($data);
print json_decode( file_get_contents($url) );

Related

How to use argv with file_get_contents or cURL or something similar

I want to pass parameters with cURL or file_get_contents and get it using argv
Using cURL
curl -X POST -H "Content-Type: application/json" -d '{"param1": "MY DATA"}' 'http://[serverIP]/file.php'
using file_get_contents
<?php
$url = 'http://[serverIP]/file.php';
$data = array('param1' => 'MY DATA');
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
print_r($result);
and I want this data as an argument - like it should print an array of arguments like
Array
(
[0] => directory/file.php
[1] => MY DATA
)
Please Note that I can use POST/GET method for getting the data but I want to know can I use argv when using curl command or anything else similar?

Instagram API curl code command to php

I can't find the curl equivalent in php for this command
curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token
I know it should start like that
$ch = curl_init('https://api.instagram.com/oauth/access_token');
curl_setopt($ch, something, ...);
...
...
One of the equivalents to
curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token
would be
$url = 'https://api.instagram.com/oauth/access_token';
$fields = array(
'client_id' => 'CLIENT_ID',
'client_secret' => 'CLIENT_SECRET',
'grant_type' => 'authorization_code',
'redirect_uri' => 'AUTHORIZATION_REDIRECT_URI',
'code' => 'CODE'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
Hope I helped! -CM

Basic Curl to PHP Conversion

I'm trying to figure out how to do this CURL command:
curl https://uploads.stripe.com/v1/files \
-u sk_test_OCHANGEDPcDr0: \
-F purpose=identity_document \
-F file="#/path/to/a/file.jpg"
In PHP, I'm using this PHP script:
https://github.com/php-mod/curl
And tried this:
$curl->setBasicAuthentication('-u', 'sk_live_CHANGED');
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => $targetFile,
));
But I think the syntax is completely wrong, I'm really new to curl and don't understand what all the different -u and -F commands PHP equivalents are.
The -u username:password option translates to:
$curl->setBasicAuthentication('username', 'password');
To upload a file, you need to put # before the filename, just like in the curl command.
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => '#' . $targetFile,
));
You'll probably want to try something like this:
$headers = array(
'Authorization: Bearer ' . $your_api_key,
'Content-Type: multipart/form-data'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://uploads.stripe.com/v1/files',
CURLOPT_POST => true,
CURLOPT_INFILESIZE => '10000',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => array(
'purpose' => 'dispute_evidence',
'file' => '#' . realpath($path_to_file)
)
));
$resp = curl_exec($curl);
curl_close($curl);
EDIT: Alternatively, you can upload files directly from the browser like this.
If you have a functional curl command invocation, you can get what you are after by adding the --libcurl filename.c option.
This will generate a C program to do what your command line did. Though you are after PHP and not C, this is still useful because they both use the same names for various options.

How to write Curl api example into PHP curl

I am using https://apptweak.io api,
they didn't mention PHP example in their code example...
please tell me, how to understand and write Curl API example into PHP curl
Here is simple "appteak.com" curl example
curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"
Here is other example
$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl
How to write these curl in PHP?
i did this in php, after google, but its not work for me.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => "my_api_key",
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump($resp ); //return false
I find correct answer
$token = "my_api_key";
$url = "http://URL.com";
$options = array('http' => array(
'header' => array("X-Apptweak-Key: $token"),
));
$context = stream_context_create($options);
$result = file_get_contents($url, 0, $context);

Convert Curl to Unirest

This is a curl request from Stripe API curl method:
curl https://api.stripe.com/v1/accounts \
-u sk_test_**********: \
-d managed=false \
-d country=US \
-d email="bob#example.com"
Right now I have this unirest code:
<?php Unirest\Request::auth(Config::get("stripe.secret.api_key"), '');
$headers = array(
"Content-Type" => "application/json"
);
$body = array(
"managed" => $_managed,
"country" => $_country,
"email" => $_email,
);
$response = Unirest\Request::post("https://api.stripe.com/v1/accounts", $headers, $body);
return array(
'status' => 'success',
'message' => $response
); ?>
Stripe returns method is wrong. I think its the -u param in curl.
I don't have much idea about Unirest. As you are facing issue with -u header, you can use the authorization parameter inside the url as below.
https://sk_test_RxRTXF1CDHuRw3ZUdynxnG6P:#api.stripe.com/v1/accounts
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Categories