Posting data using Curl in XML Format - php

Currently post my data in HTTP Post format and i need to post it in XML and add the header.
This is how i currently post my data.
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://test.com/checklead';
$fields = array(
'surname' => urlencode($surname),
'first_name' => urlencode($first_name),
'dob' => urlencode($dob),
'email' => urlencode($email),
'home_phone' => urlencode($home_phone),
'mobile_phone' => urlencode($mobile_phone),
'work_phone' => urlencode($work_phone),
'postcode' => urlencode($postcode),
'leadid' => urlencode(123),
'affid' => urlencode(123),
'subid' => urlencode(123),
'bank_acno' => urlencode($bank_acno),
'bank_sort' => urlencode($bank_sort),
'amount_required' => urlencode($amount_required),
);
//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);
How do i post this in XML Format and add a Header : http://www.header.com

You've not set Content-Type.
Try something along the lines of :
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Header1: my_header_value'
));
$output = curl_exec($ch);
or you could look at using a library such as Guzzle which does a lot of the error handling etc for you.

Related

Unable to perform a POST request in php using cURL

I am unable to perform a POST request in php, the following is my code:
$ch = curl_init();
$fields = "=var1?var2?var3";
$url = "http://localhost/Profile.php?";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
$output = curl_exec($ch);
curl_close($ch);
profile.php:
print_r($_POST);
Nothing seems to be displaying on the page just an empty array, if I do via GET it works. What have I done wrong?
Your $fields string should be in the format "key1=value1&key2=value2...", without a prepended =. So, in your case:
$fields = "key1=var1&key2=var2&key3=var3";
Additionally, the print_r($_POST) command in your code will render what has been POSTed to your page, not by your page.
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//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);

Posting to Shopify using PHP

I'm trying to post from a PHP page to Shopify to indicate that an order item has been fulfilled. I must not be doing this correctly because I keep getting a null result. I'm doing this on a test order that was cancelled, but I would expect that I'd at least get some sort of non-null response if that were the problem. Here is my code:
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.
'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$data = array('fulfillment' =>
array(
'tracking_number' => $TRACKING_NUMBER,
'tracking_company' => 'USPS',
'tracking_url' => $TRACKING_URL,
'line_items' =>
array(
'id'=>$ITEM_ID,
),
),
);
$session = curl_init( $baseUrl );
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
You are sending a json in the POST section, are you sure this is what you really want to do? The post data should be more like this, sending data1=test1&data2=test2. source
<?php
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//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($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_URL, $baseUrl);
curl_setopt($session, CURLOPT_POST, count($fields));
curl_setopt($session, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);
?>
If you really wants to send JSON via the POST fields, maybe you are missing the length of your data : source
curl_setopt($, , CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
Shouldn't you be using complete.json instead ??? I may be misunderstanding but it sounds like your trying to complete a pending fulfillment.
/admin/orders/#{id}/fulfillments/#{id}/complete.json
https://docs.shopify.com/api/fulfillment#complete
I found the problem. Shopify requires the line items to have an additional array, so instead of this:
'line_items' =>
array(
'id'=>$ITEM_ID,
)
It should be this:
'line_items' =>
array(
array(
'id'=>$ITEM_ID,
)
)

Disqus OAuth Invalid Grant

I keep getting this error when I try to use OAuth with Disqus:
{"error_description":"Invalid parameter: redirect_uri","error":"invalid_grant"}
My Code looks like this- Example 1:
$oauth2token_url = 'https://disqus.com/api/oauth/2.0/access_token/';
$redirect_uri = 'http://www.example.com/';
$clienttoken_post = array(
"grant_type" => 'authorization_code',
"client_id" => PVConfiguration::getConfiguration('disqus') -> public_key,
"client_secret" => PVConfiguration::getConfiguration('disqus') -> private_key,
"redirect_uri" => $redirect_uri,
"code" => $this -> registry -> get['code']
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
print_r($json_response);
Or this - Example 2:
$url = 'https://disqus.com/api/oauth/2.0/access_token/';
$fields = array(
'grant_type' => 'authorization_code',
'client_id' => PVConfiguration::getConfiguration('disqus') -> public_key,
'client_secret' => PVConfiguration::getConfiguration('disqus') -> private_key,
'redirect_uri' => 'http://www.example.com/',
//'scope' => 'read,write,email',
'code' => $this -> registry -> get['code'],
);
$fields_string = '';
//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);
print_r($result);
exit();
Can anyone provide any direction?
Figured it out. Documentation is here:
https://www.rfc-editor.org/rfc/rfc6749#section-4.1.3
But the problem stems from bad api error message. The redirect uri to get the code must be the exact same uri when requesting authorization.

Php : Curl unwanted output

I have Below Curl Code
<?php
$url = 'https://graph.facebook.com/me/events';
$fields = array(
'access_token' => $token,
'name' => 'Event name',
'description' => 'The Description ',
'start_time' => '2013-03-02'
);
foreach($fields as $key=>$value)
{
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
?>
In Above Code , I did not use echo $result , But its returning output as {"id":"209557021119579"}
Use this :
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
for return data instead of echoing data
you should use CURLOPT_RETURNTRANSFER ,
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

How do I use arrays in cURL POST requests

I am wondering how do I make this code support arrays? At the moment the images array only seems to send the first value.
Here is my code:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://api.example.com/api';
$fields = array(
'username' => "annonymous",
'api_key' => urlencode("1234"),
'images[]' => urlencode(base64_encode('image1')),
'images[]' => urlencode(base64_encode('image2'))
);
//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);
echo $result;
//close connection
curl_close($ch);
?>
and this is what is received at the api
VAR: username = annonymous
VAR: api_key = 1234
VAR: images = Array
array(3) {
["username"]=> string(10) "annonymous"
["api_key"]=> string(4) "1234"
["images"]=> array(1) { // this should contain 2 strings :( what is happening?
[0]=> string(8) "aW1hZ2Uy"
}
}
What is happening to the second value in images[]?
You are just creating your array incorrectly. You could use http_build_query:
$fields = array(
'username' => "annonymous",
'api_key' => urlencode("1234"),
'images' => array(
urlencode(base64_encode('image1')),
urlencode(base64_encode('image2'))
)
);
$fields_string = http_build_query($fields);
So, the entire code that you could use would be:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://api.example.com/api';
$fields = array(
'username' => "annonymous",
'api_key' => urlencode("1234"),
'images' => array(
urlencode(base64_encode('image1')),
urlencode(base64_encode('image2'))
)
);
//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_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
?>
$ch = curl_init();
$data = array(
'client_id' => 'xx',
'client_secret' => 'xx',
'redirect_uri' => $x,
'grant_type' => 'xxx',
'code' => $xx,
);
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);

Categories