how to read curl on remote server and reply - php

I want to send some data to remote server and wait for its reply,
i'm using curl to post data.
This is my client code:
$arr = array ('id' => $company , 'imei' => $drivers ,'startDate' => $startDate ,'endDate' => $endDate, 'token' => $token);
$string = http_build_query($arr);
$ch = curl_init("http://127.0.0.1/Codiad/workspace/PFE/S/server.php");
curl_setopt($ch, CURLOPT_POST , true);
curl_setopt($ch, CURLOPT_POSTFIELDS , $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
$response = curl_exec($ch);
curl_close($ch);
The server should read $string, and do some checks based on received id, imei ...
and reply to client

here is what worked for me
1- sender.php:
function curlPost($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $result;
}
print(curlPost("http://127.0.0.1/receiver.php", array("message"=>"Hello World")));
2- receiver.php:
print("received ".$_REQUEST['message']);
or
print("received ".$_POST['message']);

Related

PHP curl request reading data

I want to send a data using curl and return some data
I'am using the following code
$data = array('type' => 'active');
$result = '';
$url = 'https://example.com/'.$function_name;
$send_data = json_encode($data, TRUE);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $send_data);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);exit;
On the receiving end returning the POST data
print_r($_POST);exit;
But I'am getting data in different structure as below
(
[{"type":"active"}] =>
)
I think due to this $_POST['type'] is not getting in the receiving side

Can't hit Web API from php

I'm trying to call this
[Route("CreateHL7TestFile")]
[HttpPost]
public IActionResult CreateHL7TestFile([FromBody] JObject data)
{
// Code here
}
from a php function using cUrl. When I try and call the API without passing any data it works fine. It's when I try and add the '$data' that I cannot hit the API. When I call the API with data from Postman it also works fine. So I'm assuming my error is on the php side?
My php function looks like this.
public function CallAPI($method, $url, $data = false)
{
$data = array("value1" => "some value");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('test1' => 'value1', 'test2' => 'value2')));
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
http://localhost:50999/api/ParticipantAPI/CreateHL7TestFile
Any help would be much appreciated.
Thanks,
Adam
Use a proper model on the asp.net-core server to bind incoming data
public class MyData {
public string value1 { get; set;}
}
And update action accordingly
[Route("CreateHL7TestFile")]
[HttpPost]
public IActionResult CreateHL7TestFile([FromBody] MyData data) {
// Code here
}
Referencing the following article POSTing JSON Data With PHP cURL
Try the following on the PHP side
$data = array("value1" => "some value");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
For your real requests refactor the expected data model accordingly.
Try something like this:
public function CallAPI($method, $url, $data = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('test1' => 'value1', 'test2' => 'value2')));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}
http://php.net/manual/en/function.http-build-query.php
If you want to send json, then I think you'll have to set the header, something like:
public function CallAPI($method, $url, $data = false)
{
$dat = array("TEST1","TEST2");
$data = json_encode($dat);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
}

Curl return bool false in live server but work on local server

$data = array('paymentMethod' => 'NICEPAY', 'goodsName' => 'PROJECT FEE');
$string = http_build_query($data);
$ch = curl_init("203.99.61.173:3000/api/users/nicepay");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);
I just tried making this exact call from a local serve server and it works just fine.

cURL Request return weird value

i tried this code to post something to form
$url_server = "http://insta.kingdompanel.xyz/addlikes.php";
$postdata = array(
'url' => $url,
'comment' => $comment);
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 500 );
curl_setopt($ch,CURLOPT_URL,$url_server);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Access-Control-Allow-Origin: *','Content-type: application/x-www-form-urlencoded'));
$output = curl_exec($ch);
if ($output == FALSE){
echo "CURL ERROR".curl_error($ch);
}
curl_close($ch);
print_r($output);
but it return something like this
is it blocked?
but i read something that website cannot block cURL request.
or i did something wrong? thank you

Issue with Telegram bot developed in PHP

About a couple of months ago I could upload photos from my bot path with 5.5 version. Now I upgraded my PHP to 5.6 and I don't know why but I can't anymore.
This was my code:
$url = "https://api.telegram.org/bot".Token."/sendPhoto?chat_id=".$chat_id;
$post_fields = array(
'photo' => new CURLFile(realpath("test.png"))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type:multipart/form-data"
));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$output = curl_exec($ch);
Use this function i wrote for CURL :
function makeHTTPRequest($method, $types = []){
$url = 'https://api.telegram.org/bot'.Token.'/'.$method;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($types));
$res = curl_exec($ch);
if (curl_error($ch)){
var_dump(curl_error($ch));
} else {
return json_decode($res);
}
}
Then call it wherever you want :
var_dump(makeHTTPRequest('sendPhoto', [
'chat_id' => $chat_id,
'photo' => new CURLFile('test.png')
]));

Categories