cURL XML Post issue - php

Im posting some XML to a webservice, the code I have written works in all other cases just not in this instance.
This is my code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
$result = curl_exec($ch);
curl_close($ch);
return $result;
It keeps returning a 500 internal server error which is the same as if you go direct to the url so it's like the POST data isn't being sent. But I haven't check the content length and it seems to be..
Any Ideas?

try just this
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_string); // NOTE used $xml_string to indicate its just a text string of XML format
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
$result = curl_exec($ch);
curl_close($ch);

Related

Curl returns no data (NULL) PHP

Very simple code in PHP, that looks like this:
curl_setopt($ch, CURLOPT_URL, "http://[heres url]");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('keycode: [heres keycode]'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($output, true));
// echo $output; doesn't work as well
[heres url], [heres keycode] - url and keycode are correct
If I run the same script in a app (for example I am using a chrome app named Advanced REST Client) - everything works.
In my PHP script the result is either empty or NULL.
What could be the problem? Thanks.
$url='https://randomuser.me/api/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('keycode: [here value]'));
curl_setopt($ch, CURLOPT_URL,$url);
$html = curl_exec($ch);
var_dump(json_decode($html, true));

POST data and redirect user by PHP CURL

We need to redirect users to a URL and send some data to that URL (POST) with PHP CURL. Exactly like when a user click on HTML form submit with POST method.
Our code is
$data=array("Token"=>"test2","RedirectURL"=>"test1");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://sep.shaparak.ir/Payment.aspx');
curl_setopt($ch, CURLOPT_REFERER, 'https://sep.shaparak.ir/Payment.aspx');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POSTREDIR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_exec($ch);
But it doesn't work.
What is the solution?
As I found, it is not possible just with PHP.
You can generate a form with PHP and submit that with JS in onLoad.
Here is more detail.
This is my workable solution for post with curl:
$postdata = "email=".$username."&pass=".$password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, sizeof($postdata));
curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);
There is an alternative solution if above doesnt works:
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
curl_close ($ch);

How to api integeration in core php

I had the payment integration with paywithcapure. They send the sample code for test i used that code its 404 error.
$xml_data2 = 'merchantid=somevalue&token=somevalue&bvn=somevalue';
$URL = "http://flutterwavestaging.com:8080/FlutterwaveRecurringPayments/services/flwcardrecurring/VerifyBvn";
$ch = curl_init($URL);
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content type - application/x-www-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
First make sure that url is working fine.Paste your url in browser and see the result.If it's not working then contact them for correct url and if its working fine then try this code :
$xml_data2 = 'merchantid=somevalue&token=somevalue&bvn=somevalue';
$URL = "http://flutterwavestaging.com:8080/FlutterwaveRecurringPayments/services/flwcardrecurring/VerifyBvn";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL); // you've missed this one.
//curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content type - application/x-www-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);

Get post result with cURL and PHP

I have a little function which performs a form submission and it should return the result (html).
The form has only one text input:
<input type="text" value="" maxlength="5" name="CODE" size="10">
This is how I try to fill this variable with cURL:
//open connection
$ch = curl_init();
$myValue = "Test123";
$formUrl = "http://www.mywebsite.com/post.php";
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $formUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "CODE=".$myValue);
//execute post
$result = curl_exec($ch);
$info = curl_getinfo($ch);
//close connection
curl_close($ch);
echo $result;
My problem is that I don't get any results... Is there any problem with this code?
Thank you very much for the help!
EDIT:
I also tried with this kind of setup, with the same result:
$formData = array('CHKCODE' => $tesseractOutput);
curl_setopt($ch, CURLOPT_URL, $formUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formData));
You could try a bit different approach. A simple example:
$data = array("CODE" => "Test123");
//$ch = curl_init("http://www.mywebsite.com/post.php");
//EDIT
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
//EDIT
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/post.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$response = curl_exec($ch);
var_dump($response);
curl_close($ch);

How to send a Delete request from PHP

The best exemple i have found is:
$request_body = 'some data';
$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response);
Though this is to deltete an FB app request so the URL should look like this: https://graph.facebook.com/[request_id]?access_token=USER_ACCESS_TOKEN
Could someone show me how to implement that code for my case ?
Do not use curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
Use
$ch = curl_init('https://graph.facebook.com/'.$request_uri.'&access_token='.$access_token);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
var_dump($response);

Categories