I've used code below to send XML to my REST API. $xml_string_data contains proper XML, and it is passed well to mypi.php:
//set POST variables
$url = 'http://www.server.cu/mypi.php';
$fields = array(
'data'=>urlencode($xml_string_data)
);
//url-ify the data for the POST
$fields_string = "";
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
echo $fields_string;
//open connection
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_HTTPHEADER,array (
"Expect: "
));
//execute post
$result = #curl_exec($ch);
But when I've added other field:
$fields = array(
'method' => "methodGoPay",
'data'=>urlencode($xml_string_data)
);
It stopped to work. On the mypi.php I don't recieve any more POST parameters at all!
Could you you please tell me what to do to send XML and other post parameters in one cURL request?
Please don't suggest using any libraries, I wan't to acomplish it in plain PHP.
I don't see anything wrong with this script. It's most likely an issue with mypi.php.
You do have an extra & at the end. Maybe that confuses the server? The rtrim doesn't change the $field_string and it returns the trimmed string.
The postfields can be simplified like this,
$fields = array(
'method' => "methodGoPay",
'data'=> $xml_string_data // No encode here
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
Related
hi i wrote this script to get the html of a website but i see "Invalid Request" Error, but when i create a html form and submit it i have no problem, whats the problem? here is my php code :
<?php
$url = 'http://convert2mp3.net/en/index.php?p=convert';
$fields = array(
'url' => urlencode("www.youtube.com/watch?v=ntSBKPkk4m4"),
'format' => urlencode("3gp")
);
//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_AUTOREFERER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
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);
?>
The value of CURLOPT_POST should be a boolean, not the int you're setting it as.
Try this, if $fields can be dynamic, or just set it to boolean true:
curl_setopt($ch,CURLOPT_POST, count($fields) > 0);
Also, why are you building the POST fields yourself? Just pass the whole array to CURLOPT_POSTFIELDS:
$fields = array(
'url' => "www.youtube.com/watch?v=ntSBKPkk4m4",
'format' => "3gp"
);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
You wont success with your missions because they relate source ip address to the key of the conversation that they provide, so after you will get the direct download link in their cdn, your client's won't be able to download it because the related ip address is your web server's ip.
I currently have an API script that returns JSON. It has worked up until I tried to add in a curl php POST script before it. The curl script is working on it's own, and it is also working in the API script. However the JSON code is not being returned.
Is there something fundamentally wrong with this approach below?
Thanks in advance.
EDIT: The curl script works 100% on its own.
Said script is also working inside the below, it's just that the JSON does not return.
$name = "foo";
$age = "bar";
//set POST variables
$url = 'https://www.example.com';
$fields = array(
'name' => urlencode($name),
'age' => urlencode($age)
);
//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);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_encode(
array(
"status" => 1,
"message" => "Success!",
"request" => 10
)
);
You need to do the following use echo and also use CURLOPT_RETURNTRANSFER if not the output would be transferred directly to the page instead of $result
$name = "foo";
$age = "bar";
$url = 'http://.../a.php';
$fields = array('name' => urlencode($name),'age' => urlencode($age));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
header('Content-type: application/json');
echo json_encode(array("status" => 1,"message" => "Success!","request" => 10));
please could someone help me?
I have made a php script on my site, that will receive get data, and then return either true or false based on that get data.
I need a way to call that script through php on a completely different server, and then save the returned value.
How would I go about doing this?
Also, how would I go about doing it with POST as well?
You can use cURL to call script on other server and also post data to that script.
//set POST variables
$url = 'http://example.com/post.php';
$fields = array(
'data1' => urlencode($data1),
'data2' => urlencode($data2),
);
//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);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
More details you will get here: http://php.net/manual/en/ref.curl.php
Something like this will do it for GET:
$response = file_get_contents('your_url_here');
And for POST, I'd use cURL.
I want to process payments through my website and wanted to use sagepay for that purpose. I have gone through the guideline for sagepay go direct integration mentioned here:
http://www.sagepay.com/sites/default/files/pdf/user_guides/sagepaydirectprotocolandintegrationguidelines_0.pdf
Now,
I have created a script made necessary changes needed for the fields to process http request. here is the code:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
$txcode = 'prefix_' . time() . rand(0, 9999);
$fields = array(
'VPSProtocol'=>urlencode("2.23"),
'TxType'=>urlencode("PAYMENT"),
'Vendor'=>urlencode("myvendorname"),
'VendorTxCode'=>urlencode($txcode),
'Amount'=>urlencode("2.00"),
'Currency'=>urlencode("GBP"),
'Description'=>urlencode("payment for my site"),
'CardHolder'=>urlencode('DELTA'),
'CardNumber'=>urlencode(4929000000006),
'ExpiryDate'=>urlencode(1213),
'CV2'=>urlencode(123),
'CardType'=>urlencode('VISA'),
'BillingSurname'=>urlencode('surname'),
'BillingFirstnames'=>urlencode('name'),
'BillingAddress1'=>urlencode(' clifton'),
'BillingCity'=>urlencode('Bristol'),
'BillingPostCode'=>urlencode('BS82UE'),
'BillingCountry'=>urlencode('United Kingdom'),
'DeliverySurname'=>urlencode('surname'),
'DeliveryFirstnames'=>urlencode('name'),
'DeliveryAddress1'=>urlencode(' clifton'),
'DeliveryCity'=>urlencode('Bristol'),
'DeliveryPostCode'=>urlencode('bs82ue'),
'DeliveryCountry'=>urlencode('united kingdom'),
);
//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);
//execute post
$result = curl_exec($ch);
print_r($result);
echo "end";
//close connection
curl_close($ch);
?>
But I am not getting any response back.. what could be the problem? Its in wamp server and non https url.
This code works for me. I added a few CURL functions and removed one and that did the trick.
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://live.sagepay.com/gateway/service/vspdirect-register.vsp';
$txcode = 'prefix_' . time() . rand(0, 9999);
$fields = array(
'VPSProtocol'=>urlencode("2.23"),
'TxType'=>urlencode("PAYMENT"),
'Vendor'=>urlencode("myvendorname"),
'VendorTxCode'=>urlencode($txcode),
'Amount'=>urlencode("2.00"),
'Currency'=>urlencode("GBP"),
'Description'=>urlencode("payment for my site"),
'CardHolder'=>urlencode('DELTA'),
'CardNumber'=>urlencode(4111111111111111),
'ExpiryDate'=>urlencode(1213),
'CV2'=>urlencode(123),
'CardType'=>urlencode('VISA'),
'BillingSurname'=>urlencode('surname'),
'BillingFirstnames'=>urlencode('name'),
'BillingAddress1'=>urlencode(' clifton'),
'BillingCity'=>urlencode('Bristol'),
'BillingPostCode'=>urlencode('BS82UE'),
'BillingCountry'=>urlencode('United Kingdom'),
'DeliverySurname'=>urlencode('surname'),
'DeliveryFirstnames'=>urlencode('name'),
'DeliveryAddress1'=>urlencode(' clifton'),
'DeliveryCity'=>urlencode('Bristol'),
'DeliveryPostCode'=>urlencode('bs82ue'),
'DeliveryCountry'=>urlencode('united kingdom'),
);
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
//execute post
$result = curl_exec($ch);
print_r($result);
echo "end";
//close connection
curl_close($ch);
?>
FYI, using http_build_query() is great for building query_strings.
$fields_string = http_build_query($fields);
This code will work if you add field
Crypt='Your encryption password in your account setting in sagepay test';
But it returns null string if you use curl to post and it will be redirected to
https://test.sagepay.com/gateway/service/cardselection?vpstxid={BDBF098F-9BB5-3822-8CAE-AC0645A2CC57}
if you use Http POST.
Goodluck
Instead of printing the userinfo, the following code is printing the details of the access token granted by Google. The code is very simple yet I am not able to figure out the exact cause of this
<?php
$url = 'https://accounts.google.com/o/oauth2/token';
$fields = array(
'code'=>urlencode($_GET['code']),
'client_id'=>urlencode('###########.apps.googleusercontent.com'),
'client_secret'=>urlencode('###########'),
'redirect_uri'=>urlencode('http://######odie.co.in/googlogin'),
'grant_type'=>urlencode('authorization_code'),
);
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);
$data = json_decode($result);
print_r(file_get_contents(' https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$data['access_token']));
?>
You need to add CURLOPT_RETURNTRANSFER, true to your curl_setopt.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
refer this. its very simple.
Simple PHP Library for Google Authentication API