curl post data and get result - php

This is my code :
<?php
$url = 'http://huaweiunlockcalculator.com/v201-huawei-unlock-calculator-v3';
$fields = array('imei' => $_POST['imei']);
//url-ify the data for the POST
$fields_string="";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, 'http://huaweiunlockcalculator.com/v201-huawei-unlock-calculator-v3');
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
$output=preg_match_all("/Unlock \(V[123]\): ([0-9]+)/",$result, $matches);
echo $matches[0];
//print_r($matches[0][2]);echo "<br>";
//print_r($matches[0][1]);echo "<br>";
//print_r($matches[0][0]);echo "<br>";
curl_close($ch);
//close connection
It brings a wrong result :(
I need to send IMEI and get result in my own site. How can I do that ?

some web page need whole post data whether null of the value. A function called http_build_query() function can easily build a post or get query string , you just give a parameter type array .

$url = "http://www.example.com/api/WalletVerification";
$params = array();
$inputs = array();
$inputs['rid']=100;//$retailer_id;
$inputs['merchantid']="B170127CT3D9H2XJAQUYF0L";
$inputs['amount']=10;//$wallet_raminfo;
$params =json_encode($inputs);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$output= json_decode($result);
// print_r($output);
if($output->status=='000'){
$raminfostrans_id= $output->transactionid;
$payment_status=TRUE;
}else{
$payment_status=false;
$failure_msg="insuffient balance";
}

Related

Saving a curl response into database

I am getting this response
[{"id":15702671,"payment_id":15702666,"amount":20,"metadata":{},"source":{"id":"347862165","name":"Test Test","type":"collector"},"date_created":"2018-08-28T08:11:15.000-04:00","unique_sequence_number":null,"status":"approved"}]
This is my Code
$url = "https://api.mercadopago.com/v1/payments/".$paymeny_id."/refunds?access_token=TEST-4015220099523401-082300-232be80199d9b1a9b0bac72d6af99fd2-347862165";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);
curl_close($ch);
how to save response value in variable ?
try this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$var = curl_exec($ch)
Hope this works!
to get the id
$var = json_decode($var);
$id = $var[0]->id;
you need to add CURLOPT_RETURNTRANSFER option in your curl object to get the response using curl as
$url = "https://api.mercadopago.com/v1/payments/".$paymeny_id."/refunds?access_token=TEST-4015220099523401-082300-232be80199d9b1a9b0bac72d6af99fd2-347862165";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);

php post data using curl

When I post the data using ajax and send to post.php by curl then $_FILES variable is empty and get the data in $_POST variable. When I print $_POST var getting the following data on post.php.
[temp_upload_file] => #/tmp/php6OHgQc;filename=Penguins.jpg;type=image/jpeg
when I print "$_FILES" var getting empty data on post.php
Array
(
)
Code:
$url = "post.php";
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array('temp_upload_file' =>'#'.$_FILES['uploadfile']['tmp_name'].';filename='.$_FILES['uploadfile']['name'].';type='. $_FILES['uploadfile']['type']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
What I am doing wrong here??
Thank you in advance
For php5.5+ there is a new curl_file_create() function you need to use
the # format is now deprecated.
Try using following code:
$url = "post.php";
$tmpfile = $_FILES['temp_upload_file']['tmp_name'];
$filename = basename($_FILES['temp_upload_file']['name']);
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'uploaded_file' => curl_file_create($tmpfile, $_FILES['uploadfile']['type'], $filename)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

cURL Passing Parameters - PHP - Laravel

I am using this cURL function to send a set of data that has to be received by the api. However the api receives null.
public static function httpPost($url,$type,$params)
{
$postData = '';
foreach ($params as $k => $v) {
$postData .= $k . '=' . $v . '&';
}
$postData = rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
I have tried outputting the data before sending and it shows correct data. The other curl part works. Any idea why data is not being sent through curl?
Since you do not need to use POST, unset that curl option and use PHP http_builder to build the URL
public static function httpPost($url,$type,$params)
{
// Clean URL + append "?" at the end of URL + append the query
$url = rtrim($url,"?") . "?" . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

unable to solve curl issue

I am new to using curl. I would like you to help me creating a code for this url in curl.
http://example.com/example/example.aspx?post1=xxx&post2=xxx&post3=xxx&post4=xxx&post5=xxxxxxxx&post6=xx&post7=xxxxxxx
I want to create a php file which has to get the similar data and post the data to above url using curl. Thanks for your help once again.
<?php
$url = "http://example.com/example/example.aspx";
$fields = array(
'post1' => 'xxx',
'post2' => 'xxx',
'post3' => 'xxx',
'post4' => 'xxx',
'post5' => 'xxx',
'post6' => 'xxx',
'post7' => 'xxx'
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
Try
$data = 'post1=xxx&post2=xxx&post3=xxx&post4=xxx&post5=xxxxxxxx&post6=xx&post7=xxxxxxx';
$ch = curl_init('http://example.com/example/example.aspx');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
For more :- Passing $_POST values with cURL
and manual which you need to read :- http://php.net/manual/en/book.curl.php

Camfind API issue in PHP

For anyone familiar with the Camfind API, I've got a bit of a stumper today Implementing the API in PHP. I've followed the instructions as best I could, and I'm getting the proper token back for my first call. I'm then appending the token to the URL, removing the form data, and keeping the json header, as well as the header with my key. Whatever I try, however, returns: array(1) { ["status"]=> string(13) "not completed" }
Here's my code:
<?php
///////////////////////////////////
// FIRST REQUEST
///////////////////////////////////
//set POST variables
$url = 'https://camfind.p.mashape.com/image_requests';
$fields = array(
"image_request[altitude]" => "27.912109375",
"image_request[language]" => "en",
"image_request[latitude]" => "35.8714220766008",
"image_request[locale]" => "en_US",
"image_request[longitude]" => "14.3583203002251",
"image_request[remote_image_url]" => "https://www.google.com/images/srpr/logo11w.png"
);
$headers = array(
'X-Mashape-Key: I-put-my-real-key-here',
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result = json_decode($result, true);
///////////////////////////////////
// SECOND REQUEST
///////////////////////////////////
//set POST variables
$url = "https://camfind.p.mashape.com/image_responses/" . $result['token'];
echo $url;
$headers = array(
'X-Mashape-Key: I-put-my-real-key-here',
'Accept: application/json'
);
//open connection
$ch = curl_init();
//set the url
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
var_dump(json_decode($result, true));
Help appreciated!
Camfind support just got back to me, and made the following suggestion "Hi, it takes around 10-15 seconds for the image recognition to occur. Build in a loop with a delay before making the second call with the token, or try calling again and use a timer to re-call if not completed is still returned until it changes status to completed."
With this advice, I've updated my code as follows:
I added:
$start = microtime(true);
To the top of my file. I then wrapped my second curl call like so:
do {
//set the url
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_LOW_SPEED_TIME, 30);
$result = json_decode(curl_exec($ch), true);
} while((number_format((microtime(true) - $start), 2) < 20) && $result['status'] == 'not completed');
This is the solution, and the search now executes correctly.

Categories