http_build_query not correctly encoding my post fields - php

It's a strange issue I'm facing. Here is the CURL request I'm sending:
$fields = array(
'action' => 'json',
'region' => '',
'states' => array('TX'),
'start' => '4/2/2010',
'end' => '4/2/2017',
'internalcomments' => 'no'
);
$fields_string = http_build_query($fields);
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$jsonResult = curl_exec($ch);
curl_close($ch);
However, when I'm printing out the content of the $fields_string, I see a trademark icon in the query string:
action=json®ion=&states%5B0%5D=AL&states%5B1%5D=TX&start=04%2F01%2F2010&end=04%2F07%2F2017&internalcomments=no
Notice the ® icon after the word json? Any idea where that's coming from?

Related

consume Restful service in php?

I am trying to consume a Restful service using the below code:
$dat=array(
'entidad' => "F001",'tipoIdentificacion' => 'CC','numeroIdentificacion' => '1020442757'
);
$postdata =http_build_query( $dat );
//print_r($postdata);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
//var_dump($context);
$result=file_get_contents('http://172.18.131.195:9090/lince/rest/pazysalvo1/avaldescfin',FILE_USE_INCLUDE_PATH, $context);
//echo $result;
//$result = json_decode(file_get_contents('http://172.18.131.195:9090/lince/rest/pazysalvo1/avaldescfin?', false, $context),true);
print_r($result);
But it does not recognize the parameters that I am sending, and therefore returns null values:
{"entidad":"F001","tipoIdentificacion":null,"numeroIdentificacion":null,"codPersona":0,"estadoConsulta":"1","avales":[]}
How should I send the parameters?
I couldn't do it in this Asian way that I gave a solution in this other way.
//set POST variables
$url = 'http://172.18.131.195:9090/lince/rest/pazysalvo1/avaldescfin';
$fields = array(
'entidad' => 'F001','tipoIdentificacion' =>$tipoid,'numeroIdentificacion' => $numid
);
//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_RETURNTRANSFER, TRUE);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result=json_decode($result,true);
/*echo "<pre>";
print_r($result);
echo "</pre>";*/

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);

Collecting and using data returned from a http Curl (php) Post

I need assistance with using the data returned after a http post.
Below is my current post code that posts the data to a specific website
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://test.com';
$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);
?>
I get either one of 2 response's - {result:true} or {result:false}
I need to know is how to do the following :
When I get a {result:true} I want to redirect to url "http://thisurl.com"
When I get a {result:false} I want to return a response "Denied"
Please note the response is not json , it is just returned in that format.
You need to check what $result variable contains and then make the redirect or show denied message according to situation:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://test.com';
$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);
$result_true = 'true';
$check_true = strpos($result, $result_true);
if ($check_true !== false) {
header('Location: http://url.com');
die;
}
$result_false = 'false';
$check_false = strpos($result, $result_false);
if ($check_false !== false) {
echo "Denied";
}
And in your code you've used:
curl_setopt($ch,CURLOPT_POST, count($fields));
But in php.net manual:
CURLOPT_POST: TRUE to do a regular HTTP POST. This POST is the normal
application/x-www-form-urlencoded kind, most commonly used by HTML
forms.
So counting fields is not the proper way to do this it could be TRUE or FALSE not the count. Of course if $fields array is empty result will be 0 which is equal to FALSE or if it's 1 then it's 1 (TRUE) but when the result of the count is greater than 1 that's makes no sense.

Posting data using Curl in XML Format

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.

Writing the following curl in PHP

How would I write the following Curl in PHP?
I need to automate this process in php.
$ curl -F file=#/Users/alunny/index.html -u andrew.lunny#nitobi.com -F 'data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps
Here is the link to the Phonegap Build API.
http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps
Any help would be greatly appreciated.
This is what I have tried so far...
<?php
$url = 'https://build.phonegap.com/api/v1/apps';
$file = 'mobilecontainer.zip';
$fields = array(
'title' => 'Test App',
'create_method' => 'file',
'private' => 'false'
);
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);
curl_setopt($ch,CURLOPT_SAFE_UPLOAD, 'true');
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
You use CURL options improperly.
CURLOPT_SAFE_UPLOAD option disables support for the # prefix for
uploading files in CURLOPT_POSTFIELDS which is exactly what you
need to use.
CURLOPT_POST option expects a boolean value (true or false),
although count($fields) in your case will be evaluated to true
anyway.
-F option in the source curl command forces Content-Type value
to multipart/form-data. This mean that in PHP you have to pass
data to CURLOPT_POSTFIELDS as array. This array should contain two
elements: 'data' - json-encoded data, and 'file' - link to file
to upload.
So the code should look like this:
$url = 'https://build.phonegap.com/api/v1/apps';
$data = array(
'title' => 'Test App',
'package' => 'com.alunny.apiv1',
'create_method' => 'file',
'version' => '0.1.0',
);
$post = array(
'data' => json_encode($data),
'file' => '#mobilecontainer.zip',
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

Categories