My for loop can run twice, but it only run once when I use the cURL in for loop
My for loop can run twice, but it only run once when I use the cURL in for loop
public function index_onSync () {
$checkedIds = post('checked');
$data = ModelsAdmin::select('id', 'address', 'private_key')->whereIn('id', $checkedIds)->get();
$sync_data = array();
for ($i=0; $i < count($data); $i++) {
$url_confirm = "http://127.0.0.1:50233/sync";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_confirm);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$post_data = array( "id"=>$data[$i]['id'], "address"=>$data[$i]['address'], "private_key"=>$data[$i]['private_key'] );
$json_data = json_encode($post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($json_data)
));
curl_setopt($ch, CURLOPT_POSTFIELDS,$json_data);
$data = curl_exec($ch);
$data = json_decode($data);
array_push($sync_data, $data);
echo $data;
curl_close($ch);
}
var_dump($sync_data);
}
You're overriding your $data array from ModelsAdmin::select in line
$data = curl_exec($ch);
So, on the second iteration, your data is not a 2-rows array anymore...
Instead of $data, you could use $response, for example...
Related
I have tried everything I know and I have failed totally to parse this multi dimensional array so that I can get the "symbol" keys and corresponding values of "contractType" , but I can't seem to get it.
The array is generated here: https://fapi.binance.com/fapi/v1/exchangeInfo
So I am doing the following:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$results = [];
foreach ($data['symbols'] as $info) {
$results[] = $info['symbol'];
}
print_r($results);
I tried a foreach loop, for loop, tried various offsets eg. $data[0]['symbols']..
to $data[9]['symbols'] etc. but can't seem to get the correct array offset.
10000"}],"symbols":[{"symbol":"BTCUSDT","pair":"BTCUSDT","contractType":"PERPETUAL","deliveryDate
I'm trying to loop through the "symbols" offset within this main array, and get 1. the "symbol" 2. its corresponding "contractType"
Ty..
It doesn't work because the CURL result is in JSON.
You got to convert it to an array before you can loop through it.
$data = json_decode($data, true);
It looks like you need to convert the JSON response into an array before you loop over it.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$results = [];
$data = json_decode($data);
$i = 0;
foreach ($data->symbols as $info) {
$results[$i]['symbol'] = $info->symbol;
$results[$i]['contractType'] = $info->contractType;
$i++;
}
print_r($results);
Yes. Thankyou.. (didn't understand answer properly.)
$url = 'https://fapi.binance.com/fapi/v1/exchangeInfo';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
$data = json_decode($data, true);
$results = [];
$symbols = $data['symbols'];
$i = 0;
foreach ($symbols as $info) {
$results[$i]['symbol'] = $info['symbol'];
$results[$i]['contractType'] = $info['contractType'];
$i++;
}
print_r($results);
Much appreciated, ty..
how to send post data using curl in php array json
post curl param orderlist like an array
this is my code
<?php
class PostOrderPolicy extends CI_Controller{
public function index(){
$data = $this->getData();
foreach ($data as $key) {
$orderListt = array(
'orderDate' => $key['order_date'],
'IDNumber' => $key['IDnumber'],
'itemDescription' => $key['description']
);
$send = $this->sendData($orderListt, $key['resi_number']);
if ($send) {
$res["message"] = "Sukses";
}else{
$res["message"] = "Gagal";
}
}
//echo json_encode($res);
}
private function getData(){
$sql = "SELECT * FROM t_transaction WHERE status='00'";
return $this->db->query($sql)->result_array();
}
private function sendData($data){
$result = array();
$url = "https://contoh.co.id/createPolicy";
$res = array();
$sign = $this->getSign();
$upperSign = strtoupper($sign);
//$random = $this->getRandomStr();
$random = '049KN1PSOL16QHIF';
$batch = 'BATCH000002';
$dataArr = array(
'sign' => $upperSign,
'randomStr' => $random,
'batchNo' => $batch,
'orderList' => [$data]
);
$string = json_encode($dataArr);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$response = json_decode(curl_exec($ch), TRUE);
print_r($response);
print_r($string);
}
}
?>
but my result post field
[enter image description here][1]
[1]: https://i.stack.imgur.com/b7LEP.png
i want like this
[enter image description here][2]
[2]: https://i.stack.imgur.com/xExJ2.png
{"sign":"35EAF78F95A52DDA79FC9911559026F6","randomStr":"049KN1PSOL16QHIF","batchNo":"BATCH000002","orderList":[{"orderDate":"2020-07-01 11:00:00.000","resiNumber":"99875646957","itemDescription":"testing"},{"orderDate":"2020-07-01 ]11:00:00.000","IDNumber":"28929740170","itemDescription":"testing"}]}
Return response instead of outputting:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Execute the POST request:
$result = curl_exec($ch);
Close cURL resource:
curl_close($ch);
I have this code
I need a loop to be able to print all results which are 400-600 but I'm not able because I'm limited to only 100 shows per page and I need a loop/while to make the same request but changing the page and then print the result and I'm not able to make it, can someone explain me how to implement here a loop or to give me some examples?
<?php
include('config/config.php');
$ch = curl_init();
$username = '**************';
$password = ''**************';';
$hash = base64_encode($username . ':' . $password);
$headers = array(
'Authorization: Basic ' . $hash
);
$query = $connection->query("SELECT id_xyz_ro as prods FROM stock_prods where id_xyz_ro > 0");
$array = Array();
while($result = $query->fetch_assoc()){
$array[] = $result['prods'];
}
$connection->query('SET session group_concat_max_len=15000');
foreach($connection->query("SELECT GROUP_CONCAT(CONCAT('\'', id_xyz_ro, '\'')) as prods FROM stock_prods where id_xyz_ro > 0") as $row) {
}
$datas = array(
'id' => $array,
'itemsPerPage' => '100', //Only 100 because API it's limiting me at 100 shows per page
'currentPage' => '1' //Here I need to loop
);
$data = array(
'data' => $datas
);
set_time_limit(0);
$url = "www.xyz.net/api-3/product_offer/read";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r($result); //After loop I need to print results from all loops
?>
Thanks in advance
I wanted to make an inline bot! and when i do this:
function sendResponse($url, $data){
$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_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('inline_query_id' => $data['inline_query_id'], 'results' => json_encode($data['results'])));
$output = curl_exec($ch);
return $output;
}
It wont work, the error (with or without the header): {"ok":false,"error_code":400,"description":"[Error]: Bad request: Field \"message_text\" must be of type String"}
but when I do it like this:
function sendResponse($url, $data){
$ch = curl_init();
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($ch, CURLOPT_URL, $url.'?inline_query_id='.rawurlencode($data['inline_query_id']).'&results='.rawurlencode(json_encode($data['results'])));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $q);
$output = curl_exec($ch);
return $output;
}
It works ! the problem is the second method request URI will be too large so I cannot use it!
Any way I can send these data is okay with me! thanks!
and the code for making $data is here:
$result = connectWebsite(SITE_SEARCH_URL, urlencode($update['inline_query']['query']));
$result = json_decode($result);
$output = array();
$output['inline_query_id'] = $update['inline_query']['id'];
$i = 0;
foreach($result as $post){
$data = array();
$data['type'] = 'article';
$data['id'] = strval($post->ID);
$data['title'] = '('.$post->atypes.') '.$post->title;
if(strlen($post->content) > 2100)
$tmp = substr($post->content, 0, 2096).'...';
$data['message_text'] = '<b>'.$post->title.'</b>'.ucwords($post->genre, ',').$tmp;
$data['parse_mode'] = 'HTML';
if(strlen($post->content) > 200)
$tmp = substr($post->content, 0, 196).'...';
//$data['description'] = ucwords($post->genre, ',').' | '.$tmp;
$output['results'][$i] = $data;
$i++;
if($i == MAX_RESULTS)
break;
}
sendResponse(API_URL.'answerInlineQuery', $output);
It might help someone so I'll answer it myself.
the problem was the UTF-8 encoding
I replaced substr with mb_substr
besides at the first line I'v added this: mb_internal_encoding("UTF-8")
and ... the problem was solved. now I can send my inline query results (or any other command) without the URL length problem
Thanks everyone for your help
I am using this function for calling method from one server to another in PHP.
function get_url($request_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
}
$request_url = 'http://second-server-address/listening_page.php?function=somefunction';
$response = get_url($request_url);
Here I am giving a URL with function name. The question is what if the function receives few parameters? How would we pass parameters to the method on another server using CURL.
Just add
$request_url = 'http://second-server-address/listening_page.php?function=somefunction&funcParam1=val&funcParam2.val
Use these passed parameters in your function
If you want to pass parameter as post request then try this.
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
}
$data = array(
"name" => "c.bavota",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan"
);
post_to_url("http://yoursite.com/post-to-page.php", $data);