This question already has answers here:
How can I debug exec() problems?
(4 answers)
sudo in php exec()
(8 answers)
How can I get useful error messages in PHP?
(41 answers)
PHP - Debugging Curl
(8 answers)
Closed 3 years ago.
I am using curl as like below.
http://99.88.77.121/sample/curl.php
$ch = curl_init();
$ip = "Some IP address";
curl_setopt($ch, CURLOPT_URL, 'http://99.88.77.121/test/index.php');
curl_setopt($ch, CURLOPT_POST, 1);
$payload = json_encode(
array(
'username' => 'abc',
'password' => '1234'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("REMOTE_ADDR: $ip", "HTTP_X_FORWARDED_FOR: $ip"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
The above code sends the data to the Url.
I have code like below in my http://99.88.77.121/test/index.php
<?php
$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);
$data = json_decode($rawData);
$username = $data->username;
$password = $data->password;
$projectName = "testFolder";
chdir('../');
//chdir($projectName);
$command = "sudo chmod -R 777 ".$projectName;
shell_exec($command);
?>
I am trying give full permission for testFolder using above code,
is chdir() and shel_exec() will work here? In my case its not working
If i run the index.php in browser, its working fine. but not from the curl call.
Can anyone look into it and update me your thoughts please.
Related
I'm recently started developing on moodle and one big problem is that whenever i build a plugin with a web page, it don't execute any of my curl commands, i hear that moodle has his own php library(an php file called filelib.php i suppose) with curl commands and i should be using its commands instead of mine, but i don't have much experience with moodle development and php so all that code make me very confused.
i tried this question before and a gentleman helped me with an example, i tried his code and it didn't work(it changed the output but still show an error message, i also tried changing the code to no avail) and i didn't get much of an explanation about how curl commands work on moodle(i'm still grateful for his help) or whats commands on filelib.php will substitue my code, can someone help me?
my code:
<?php
require_once(__DIR__.'/../../config.php');
require_once($CFG->libdir.'/filelib.php');
$PAGE->set_url(new moodle_url(url:'/local/iesde/selecionaraulas.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title('selecionar aulas');
$PAGE->requires->js_call_amd('local_iesde/tabelas', 'init');
echo $OUTPUT->header();
echo $OUTPUT->render_from_template('local_iesde/manageaulas', $templatecontext);
$api_server = 'url';
$api_http_user = 'user';
$api_http_pass = 'pass';
$key_acess = 'key';
$key_name = 'API-KEY';
$format = 'json';
$params = array(
'IDstudent' => 000000,
'IDcourse' => 000000,
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "{$api_server}/format/{$format}");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($curl, CURLOPT_USERPWD, "{$api_http_user}:{$api_http_pass}");
curl_setopt($curl, CURLOPT_HTTPHEADER, array("{$key_name}:{$key_acess}"));
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_exec($curl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($curl);
var_dump(json_decode($output));
echo $output;
echo $OUTPUT->footer()
the purpose of this curl request is to print a list of ids that will be used on another curl request to generate a video file that will be executed on a videoplayer, the variables:
$api_server = 'url';
$api_http_user = 'user';
$api_http_pass = 'pass';
$key_acess = 'key';
$key_name = 'API-KEY';
$format = 'json';
$params = array(
'IDstudent' => 000000,
'IDcourse' => 000000,
);
Are all needed to execute the commands (one of the reasons i think the gentleman's example didn't work is that it didn't used all the variables of the code to get full validation).
well that is all, i'm very new to all of this so any explanation will help, thanks for the help.
This question already has answers here:
Get JSON object from URL
(11 answers)
Closed 2 years ago.
I am creating a Courier tracking plugin and get data using their API. The output they are returned is in JSON format. Here is the courier API
$curl_handle = curl_init();
// For Direct Link Access use below commented link
//curl_setopt($curl_handle, CURLOPT_URL, 'http://new.leopardscod.com/webservice/trackBookedPacket/?api_key=XXXX&api_password=XXXX&track_numbers=XXXXXXXX'); // For Get Mother/Direct Link
curl_setopt($curl_handle, CURLOPT_URL, 'http://new.leopardscod.com/webservice/trackBookedPacket/format/json/'); // Write here Test or Production Link
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array(
'api_key' => 'your_api_key'
'api_password' => 'your_api_password'
'track_numbers' => 'string' // E.g. 'XXYYYYYYYY' OR 'XXYYYYYYYY,XXYYYYYYYY,XXYYYYYY' 10 Digits each number
));
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
How to echo the values get from this curl command?
Regards
Please use json_decode function after $buffer = curl_exec($curl_handle);
$buffer = curl_exec($curl_handle);
$json = json_decode($buffer, true);
print_r($json);
You can get PHP object array
This question already has answers here:
Receive JSON POST with PHP
(12 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I need to execute some code present inside one PHP file and this file will invoked from other PHP file using CURL but simply as per my code its not happening like that.
I am explaining my code below.
test.php:
<?php
include('db.php');
$adminUrl1='http://example.com/api/product/test1.php';
$data1 = array("user_id" => 54, "org_ord_id" => 82);
$data_string1 = json_encode($data1);
$ch1 = curl_init($adminUrl1);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch1, CURLOPT_POSTFIELDS, $data_string1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string1))
);
$res = curl_exec($ch1);
$array = json_decode($res, 1);
echo($array);exit;
?>
Here I am trying to execute the test1.php file which is present inside api/product/ directory.
test1.php:
include('db.php');
$user_id=$_POST['user_id'];
$order_id=$_POST['org_ord_id'];
echo $user_id.'::::'.$order_id;
Here I need to print those post param value those are passed from test.php but as per my current code its not printing anything.
Remove this 'Content-Type: application/json', because you need to get values like application/x-www-form-urlencoded for $_POST[...].
And put application/json to CURLOPT_HEADERFUNCTION for response.
CURL_OPT DOC
This question already has answers here:
How to write into a file in PHP?
(9 answers)
Closed 5 years ago.
I am a complete noob when working with APIs and PHP.
I want to have the following code dump results into a file called books.json
and I have no idea how to do it. Any help is greatly appreciated!
// Built by LucyBot. www.lucybot.com
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$query = array(
"api-key" => "MY APRI KEY"
);
curl_setopt($curl, CURLOPT_URL,
"https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json" . "?" . http_build_query($query)
);
$result = json_decode(curl_exec($curl));
echo json_encode($result);
Have a look at file_put_contents:
file_put_contents($path_to_file, $result);
I'm trying to run CURL command in PHP to upload image to an API
the code mentioned in the doc was:
curl -u 15:tokenkeyiskmzwa8awaa https://api.bukalapak.com/v2/images.json -F file=#product-image.png -X POST
Have tried using mac terminal running this curl command (with proper username and password) and successfully got the result as it is uploading, however was not successfully doing it on PHP.
My php code is :
<?php
$data = array('file'=> '#'.$imagePath );
// this is an absolute path, give something like D://folder/path/on/my/webserver/image.jpg
$user = '1234567';
$pass = '123456';
$ch = curl_init();
$curl_options[CURLOPT_URL] = 'https://api.bukalapak.com/v2/images.json';
$curl_options[CURLOPT_CAINFO] = storage_path('app/cacert.pem');
$curl_options[CURLOPT_HEADER] = "Content-Type: application/x-www-form-urlencoded";
$curl_options[CURLOPT_POST] = 1;
$curl_options[CURLOPT_USERPWD] = $user.':'.$pass;
$curl_options[CURLOPT_POSTFIELDS] = http_build_query($data);
curl_setopt_array($ch, $curl_options);
$content = curl_exec($->ch);`
Curl version : 7.47.1
PHP version : 5.6.21
any feedback is very appreciated.
You did not posted the complete script: $curl_options is undefined, storage_path is related to Laravel but there is no import, etc.
Something like the following script should work as you expect (I didn't tested it since I don't have an account on this service):
<?php
$imagePath = 'path/to/your/image.ext';
$user = 'youruser';
$pass = 'yourpass';
$file = curl_file_create($imagePath);
$body = ['file' => $file];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.bukalapak.com/v2/images.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($ch, CURLOPT_CAINFO, 'path/to/cacert.pem');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
curl_close($ch);