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
Related
I am trying to POST a file from URL using PHP cURL to the another server and getting an error:
There was error in processing your request.
Here is my code:
$url = 'http://www.example.com/api/uploads';
$fields = array('id' => $apikey,'token' => $token ,'myFile' => array(urlencode(base64_encode(file_get_contents("http://www.pdf995.com/samples/pdf.pdf")))));
$data_json = json_encode($fields);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data', 'cache-control: max-age'));
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
Field should be like this if you want to upload a file.
// file from your local directory
$fields = array('id' => $apikey,'token' => $token ,'myFile' => "#/samples/pdf.pdf");
And the post fields should be:
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
Remove the option CURLOPT_HTTPHEADER.
I am trying to call some http web-service with php cURL. I can make the request and get successful response when I try calling that service in the browser. But whenever I try to call it via cURL, it fails with error message: couldn't connect to host
The code I am using here is as follows:
$url = 'http://myserviceurl.com';
$fields = array(
'field1' => urlencode('field1value'),
'field2' => urlencode('field2value'),
'field3' => urlencode('field3value')
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
if($result === false)
{
echo sprintf('<span>%s</span>CURL error:',curl_error($ch));
return;
}
How can I fix/diagnose this?
This could happen if you have a browser based http proxy set
Did you try setting SSL_VERIFYER to false?
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
#Bijoy(In the another answer) is correct. There was a proxy set in the browser. I changed the code to add the proxy with CURLOPT_PROXY like this:
$proxy = '<my-proxy-ip>:<proxy-port>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
Now it works.
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";
}
Hi i need to make a curl Request via PHP passing a XML by POST but I have no idea how to do that anyone have any idea ?
what i have right now
$xml = '<project>
...
</project>';
$url = 'http://login:token#localhost:8080/createItem?name=newjobname';
$fields = array(
'name' => urlencode('newjobname'),
);
$fields_string = 'name=newjobname';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$result = curl_exec($ch);
curl_close($ch);
Remove the login from the $url and add this to your curl:
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "login:token");
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.