I am developing an Instagram application. Nevermind, my questions isn't focused on the API.
Its a simple php question. However. Everytime I try to run the the output is that the TOKEN is empty.
$token = 'XxXXXXXXXXX.XXXXXXX.XXXXXX';
$access_token_parameters = array(
'access_token' => $token,
'action' => 'like'
);
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_GET,true);
curl_setopt($curl,CURLOPT_GETFIELDS,$access_token_parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($curl);
}
Is there a way to set a value externally the Array? (Between, I tried it with include file.php. which works.) But I would like to call this without using an external php file.
Hope you understand my problem and may help me :)
$token = 'XxXXXXXXXXX.XXXXXXX.XXXXXX';
$access_token_parameters = array(
'access_token' => $token,
'action' => 'like'
);
$curl = curl_init($url."?".http_build_query($access_token_parameters ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($curl);
}
Related
I'm working on a wordpress project where I have to modify my theme, so I can request a JSON to an external API.
I've been searching through the internet how to do that and a lot of people use CURL.
I must do a POST request, yet I don't know how it works or how to do it.
So far I've got this code running:
$url='api.example.com/v1/property/search/';
$data_array = array(
$id_company => '123456',
$api_token => 'abcd_efgh_ijkl_mnop',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_array);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: 111111111111111111111',
'Content-Type: application/json'
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo($result);
I don't know where exactly I should put my authentication info or how does the curl methods work in PHP. Can you guys check it out and help me solve this?
There are some answers out there that would help you, such as this one.
However, WordPress actually has built-in functions to make GET and POST requests (that actually fall back to cURL I believe?) named wp_remote_get() and wp_remote_post(). Clearly in your case, you'll want to make use of wp_remote_post().
$url = 'https://api.example.com/v1/property/search/';
$data_array = array(
'id_company' => 123456,
'api_token' => 'abcde_fgh'
);
$headers = array(
'APIKEY' => 1111111111,
'Content-Type' => 'application/json'
);
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers,
'body' => $data_array,
'cookies' => array()
)
);
if( is_wp_error( $response ) ){
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Success! Response:<pre>';
print_r( $response );
echo '</pre>';
}
I've started making a webpage that uses an API from another website to get phone numbers for verification of websites, which I've gotten working with Python already. However, when I try to use cURL to get the JSON data from the API it returns nothing. The code for the PHP is below.
echo "Requesting number...";
$url = 'api.php'; # changed from the actual website
$params = array(
'metod' => 'get_number', # misspelt because it is not an english website
'apikey' => 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'country' => 'RU',
'service' => 'XXXXX',
);
$ch = curl_unit();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
if(curl_errno($ch) !== 0) {
error_log('cURL error when connecting to ' . $url . ': ' . curl_error($ch));
}
curl_close($ch);
print_r($result);`
I expect that when the code is executed on the server it should give all of the JSON from the file, which I can then use later to pick out only certain parts of it to use elsewhere. However the actual results are that it does not print anything, as seen here: https://imgur.com/sdCYhlw
I'm not sure why your code doesn't work, but a simpler alternative could be to use file_get_contents:
echo "Requesting number...";
$url = 'api.php'; # changed from the actual website
$postdata = http_build_query(
array(
'metod' => 'get_number', # misspelled because it is not an English website
'apikey' => 'XXXXXXXXXXXXXXXXXXXXXXXXX',
'country' => 'RU',
'service' => 'XXXXX'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
print_r($result);
I'm trying to post to an API using cURL with no luck. I've been researching this for 2-days now and I can't seem to get it to work.
Here is an example of a URL that I can paste into a web browser and it works, no problem:
http://{myserver}:{port}/api.aspx?Action=AddTicket&Key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&Subject=Test&Description=Test&Username={domain}\{username}
(I obviously redacted some information).
I know that cURL is up to date and working on the server because I can make a simple request out to [http://www.google.com] and it returns the page properly and I've also confirmed it on the php.info page as being ENABLED.
I've tried every layout I can find for the cURL code such as settings the POSTFIELDS as an array as well as a string. I've followed along with multiple YouTube videos and web tutorials to the 'T' with no success. I've even tried setting the URL parameter in the $ch to the entire above URL just for the heck of it... No success.
Can anyone explain or give examples of how this should be formatted so that it simply posts a URL identical to the one above??
Much appreciated!
As requested, here's my code.
$url = 'http://{server}:{port}/api.aspx?Action=AddTicket';
$post_data = '&key=' . $key . '&subject=' . $subject . '&description=' . $details . '&username={domain}\{username}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
if ($output === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
print_r($output);
And here is my attempt using an array instead of a string for the POSTFIELDS.
$url = 'http://{server}:{port}/api.aspx?Action=AddTicket';
$post_data = array(
'key' => $key,
'subject' => $subject,
'description' => $details,
'username' => '{domain}\{username}'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$output = curl_exec($ch);
if ($output === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
print_r($output);
EDIT
I've tried some of the examples given in the comments. Here's a small test I'm currently running.
$data = array(
'Action' => 'AddTicket',
'Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
'Subject' => 'test',
'Description' => 'test',
'Username' => '{domain}\{username}'
);
$query = http_build_query($data);
$url = 'http://{server}:{port}/api.aspx?' . $query;
print_r($url);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_URL => $url
));
$resp = curl_exec($curl);
curl_close($curl);
Now, if I straight up take the $url variable from the above code and run this...
header("Location:" . $url);
die();
It works perfectly... so my problem has to be something in the cURL syntax or parameters...
EDIT
After adding the following code...
var_dump($resp);
var_dump(curl_getinfo($curl, CURLINFO_HTTP_CODE));
var_dump(curl_error($curl));
I get the following result...
string(0) ""
int(401)
string(0) ""
Anyone know what this means?
Your working example indicates that this is not a POST request at all, but instead a GET request.
Try building your URL like so:
$data = array(
'Action' => 'AddTicket',
'key' => $key,
'subject' => $subject,
'description' => $details,
'username' => '{domain}\{username}'
);
$query = http_build_query($data);
$url = 'http://{myserver}:{port}/api.aspx?' . $query;
Then you probably only need to perform a GET request to that URL.
Edit: Seeing your latest update, try using json_encode on your postfields.
The URL you're supplying uses GET parameters. Hopefully the below snippet should help;
$curl = curl_init();
// Heres our POSTFIELDS
$params = array(
'param1' => 'blah1',
'param2' => 'blah2'
);
$params_json = json_encode($params);
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://example.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPAUTH => CURLAUTH_ANY,
CURLOPT_POSTFIELDS => $params_json
));
$response = curl_exec($curl);
$error = curl_error($curl);
curl_close($curl);
I want to post to a Synology Disksation and upload a file with PHP. The Synology's API gives me an interface that I want to call with PHP. See the documentation below.
https://global.download.synology.com/download/Document/DeveloperGuide/Synology_File_Station_API_Guide.pdf#%5B%7B%22num%22%3A111%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C69%2C711%2C0%5D
Here is my code, which I use for the post:
<?php
$params = array(
'path' => '/home/upload',
'create_parents' => 'true',
'overwrite' => 'true',
'api' => 'SYNO.FileStation.Upload',
'version' => 2,
'method' => 'upload'
'_sid' => [id of session after authenticate],
'file[]' => "#".path_to_file
);
$ch = curl_init();
$BODY = http_build_query($params);
curl_setopt($ch, CURLOPT_URL, 'http://ip_of_diskstation:5000/entry.cgi');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
?>
This call works, but the diskstation gives me the following JSON.
{"error":{"code":401},"success":false}
According to the documentation, there is a "Unknown error of file operation". Also, using "file_get_contents (path to file)" instead of "#".path_to_file gives me the same error from the diskstation.
How can I post a file and parameters to a synology diskstation?
Can you try to add _sid in your web api url like http://ip_of_diskstation:5000/entry.cgi?_sid=
I make it work using nodejs request library.
Below is the code:
let r = request.post( { url: `${web_url}/entry.cgi?_sid=${sid}` }, ( err, response, body ) => {
return callback( err, response );
});
let form = r.form();
form.append( 'api', 'SYNO.FileStation.Upload');
form.append( 'version', '2');
form.append( 'method', 'upload' );
form.append( 'overwrite', 'false' );
form.append( 'path', '<shared folder path>');
form.append( 'create_parents', 'true' );
form.append( '_sid', sid );
form.append( 'file', fs.createReadStream( file ), { 'filename' : <filename without the path>});
Regards,
Felix
You get :
"{"error":{"code":401},"success":false}"
Because the official document is wrong.
See Upload file through FileStation upload api for details.
From what I see the url that you used in CURLOPT_URL needs to be updated. Also the file that is in the $params needs to be updated.
Some side notes. Make sure your settings for the user's SID has permissions set properly. Also make sure you updated the $_FILES["uploadfile"] to include whatever name you used within the input element of the form (instead of uploadfile, which I used).
See this github link for where I found my reference. Postman helped me duplicate this in PHP.
$sid = '_sid=[YOURSID]';
$url = 'HOSTNAME/webapi/entry.cgi?' . $sid;
$SynologySharedPath = 'FILEPATH';
$params = array(
'api' => 'SYNO.FileStation.Upload',
'version' => '2',
'method' => 'upload',
'path' => $SynologySharedPath,
'create_parents' => 'true',
'overwrite' => 'true',
'file' => new \CurlFile($_FILES["uploadfile"]["tmp_name"], $_FILES["uploadfile"]["type"], $_FILES['uploadfile']['name'])
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE,true);
$result = curl_exec ($ch);
$curlresponse = json_decode($result, true);
What I want to do is to send an "identify" request to "threads.io" using php, however, I didn't find a way to do it (I couldn't even find a "threads.io" documentation for php). Does anyone know the proper way to do it? (syntax, etc.)
You'll have to use curl. Here's a basic script that will do it.
// You must use UTC timezone
date_default_timezone_set('UTC');
$project_key = 'your project key';
$data = array(
'userId' => '12',
'traits' => array(
'name' => 'Jeff',
'email' => 'jeff#example.com',
),
'timestamp' => date('Y-m-d\TH:i:s') . '.000Z',
);
$data = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://input.threads.io/v1/identify");
curl_setopt($curl, CURLOPT_USERPWD, "$project_key:''");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);
A good starting place to understanding curl is: http://www.hackingwithphp.com/15/10/2/your-first-curl-scripts
And of course php.net: http://php.net/curl