VB to PHP Curl posting xml - php

I am trying to convert this VB script to PHP curl
xmlServerHttp.open "POST","url",False xmlServerHttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlServerHttp.send "xmlmessage=" & Server.URLEncode(xmlDocument)
‘ xmlDocument = the Xml Document contain the actual request
xmlServerStatus = xmlServerHttp.status
if xmlServerStatus = "200" then
xmlServerResponse = xmlServerHttp.responseText
Else
Response.Appendtolog ".xmlServer status is " & xmlServerStatus
end if
This is what I have so far however it is failing
$curl = curl_init(url);
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded') ,
CURLOPT_POSTFIELDS => $xmldoc,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);
// Setting curl options
curl_setopt_array( $curl, $options );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
// Getting results
echo curl_exec($curl);
The API i am calling return that the xmlmessage variable is not a valid xml document.

try to change $xmlDoc to $xmlDoc->asXML()
like this
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded') ,
CURLOPT_POSTFIELDS => array('xmlmessage='=> $xmlDoc),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
);

Related

Why is my PHP code to copy a folder to another folder on box.com not working?

I am using php curl to copy a folder to another folder (to become a subfolder inside the second folder). Here is the curl option array (CODE value hidden) followed by the curl operations:
array (
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_VERBOSE => false,
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTPHEADER =>
array (
'Authorization: Bearer CODE',
'Content-Type: application/json',
'Content-Length: '.$len,
),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => {"name":"Newfolder","parent":{"id":"$id"}}
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
The result is '1'.
You need to set
CURLOPT_RETURNTRANSFER => true
Quoting from PHP Manual
true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.

How to setup correct cURL request for X Post? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have an API to track the shipment, the API request is below and worked well as excepted,
curl -X POST
--header '17token:xxxxxxxxxxxxxxxxxxxxxxxxx'
--header 'Content-Type:application/json'
--data '[{"number":"RR123456789CN"}]'
https://api.17track.net/track/v1/register
Here is my cURL code to get the response.
<?php
// started curl
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.17track.net/track/v1/register",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "number:RR123456789CN", // SOMETHING IS WRONG HERE.
//data '[{"number":"RR123456789CN"}]'
CURLOPT_HTTPHEADER => [
"17token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"header 'Content-Type:application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
The above code is showing error.
{"code":0,"data":{"errors":[{"code":-18010013,"message":"Submitted data is invalid."}]}}
I have tried all kinds of data structures like :
1. CURLOPT_POSTFIELDS => "RR123456789CN",
2. CURLOPT_POSTFIELDS => "[{number:RR123456789CN}]",
3. CURLOPT_POSTFIELDS => "{number:RR123456789CN}",
4. CURLOPT_POSTFIELDS => '{number:RR123456789CN}',
5. CURLOPT_POSTFIELDS => '{number:RR123456789CN}',
6. CURLOPT_POSTFIELDS => "data '[{number:RR123456789CN}]'",
7. CURLOPT_POSTFIELDS => "{\"number\": \"RR123456789CN\"}",
8. CURLOPT_POSTFIELDS => "[{\"number\": \"RR123456789CN\"}]",
but all the above data structures are failing and showing the same error.
I think I am doing something wrong with X POST / POST or
I am failing to request properly.
Edit
After doing some research I have written new code..
<?php
// setting the main url
$url = 'https://api.17track.net/track/v1/register';
// the tracking id as array
$post_data = array (
'number' => 'RR123456789CN',);
// start
$ch = curl_init();
// URL
curl_setopt($ch, CURLOPT_URL, $url);
// Returntransfer = true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Post = true
curl_setopt($ch, CURLOPT_POST, 1);
// Header # 1
curl_setopt($ch, CURLOPT_HEADER, "17token:xxxxxxxxxxxxxxxxxxxxxxxxx");
// Header # 2
curl_setopt($ch, CURLOPT_HEADER, 'Content-Type:application/json');
// posting tracking id
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Getting data in a string ($output)
$output = curl_exec($ch);
// error msg if fails
if ($output === FALSE){
echo "cURL Error:" . curl_error($ch);
}
// turning off the cURL
curl_close($ch);
// Printing the output.
print_r($output);
?>
Now i am getting {"code":401,"data":{"errors":[{"code":-18010002,"message":"Access token is invalid."}]}} error.
Note : Token is valid and correct, i think something is wrong with my code.
Thank you.
You have to send your data as raw body:
$curl = curl_init();
$data = array(0 => array("number" => "CM436202796IN"));
$data = json_encode($data);
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.17track.net/track/v1/register',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data, // Mistake is here
CURLOPT_HTTPHEADER => array(
'17token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
replace
CURLOPT_CUSTOMREQUEST => "POST",
with just
CURLOPT_POST=>1
and replace
CURLOPT_POSTFIELDS => "number:RR123456789CN", // SOMETHING IS WRONG HERE.
with
CURLOPT_POSTFIELDS => json_encode(array(
0 => array(
"number" => "CM436202796IN"
)
)),
and replace
CURLOPT_HTTPHEADER => [
"17token:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"header 'Content-Type:application/json"
],
with
CURLOPT_HTTPHEADER => [
"17token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type: application/json"
],

Error on cURL request using php-cURL while sending SMS

The request entity's media type 'multipart/form-data' is not
supported for this resource.\",\"ExceptionMessage\"unsure emoticon"No
MediaTypeFormatter is available to read an object of type 'SmsQueue'
from content with media type
'multipart/form-data'.\",\"ExceptionType\"unsure
emoticon"System.Net.Http.UnsupportedMediaTypeException\",\"StackTrace\"unsure
emoticon" at
System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent
content, Type type, IEnumerable 1 formatters, IFormatterLogger
formatterLogger, CancellationToken cancellationToken)\r\n at
System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage
request, Type type, IEnumerable`1 formatters, IFormatterLogger
formatterLogger, CancellationToken cancellationToken)\"}"
// Prepare you post parameters
$postArray = array(
'APIKey' => AUTH_KEY,
'number' => $mobile,
'text' => $message,
'senderid' => SENDER_ID,
'channel' => $channel,
'DCS' => $DCS,
'flashsms' => $flashsms,
'route' => $route
);
// Init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postArray
));
// Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Get response
$curlOutput = curl_exec($ch);
// Print error if any
if (curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
curl_close($ch);
CURLOPT_POSTFIELDS => http_build_query($postArray) not working
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS => http_build_query($postRequest1));
Set Content-Type in header to application/x-www-form-urlencoded.
Use like this :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://url.com",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query(['message' => test]),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));

PHP cURL not taking time outs into account

I have a simple PHP script that uses cURL to grab the contents of a URL:
$curlOptions = [
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HEADER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 5,
CURLOPT_NOBODY => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
];
$curl = curl_init();
curl_setopt_array( $curl, $curlOptions );
$curlResult = curl_exec( $curl );
$status = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
curl_close( $curl );
However, in some circumstances I clearly see durations of 20 seconds or more eventhough I specified lower time out values for CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT.
How can I make cURL time out at the values I specified?
UPDATE:
All the cURL's that take 20 seconds to complete return a "status" of 0. Its probably a DNS problem where it can't resolve the host. I would assume that CURLOPT_CONNECTTIMEOUT would take care of this?
It seems that your code works perfectly.
I've created a simple sleep page, you can check it (gets sleep time as input).
In case that you find cases where the code did not time out, I suggest checking the status code, and to see whether the code got hung on other part (maybe not the curl itself).
<?php
$url = "http://funnify.me/sleep.php?sleep=12"; // time in seconds
$curlOptions = [
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HEADER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 5,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 8,
CURLOPT_NOBODY => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
];
$curl = curl_init();
curl_setopt_array( $curl, $curlOptions );
$curlResult = curl_exec( $curl );
$status = curl_getinfo( $curl, CURLINFO_HTTP_CODE );
curl_close( $curl );
echo $status;
?>
Cheers,
Ika

request URL with parameters in curl php

I want to request for a certain web page content for particular option selected from drop down list.
In my example, I want content from web page where Community and Level are two drop down lists. and I want web page for option Community='SoftwareFactory/Cloude' and Level='V6R2015x'.
My code is
<?php
// init the resource
$ch = curl_init('http://e4allds/');
// set a single option...
$postData = array(
'Community' => 'SoftwareFactory/Cloud',
'Level' => 'V6R2015x'
);
curl_setopt_array(
$ch, array(
CURLOPT_URL => 'http://e4allds/',
CURLOPT_POSTFIELDS => $postData,
//OPTION1=> 'Community=SOftwareFactory/Cloud',
//OPTION2=> 'Level=V6R2015x',
CURLOPT_RETURNTRANSFER => true
));
$output = curl_exec($ch);
echo $output;
But its giving result for default selection. Could anyone please help me how can I pass these parameters to the URL?
You need to enable the cURL POST parameter to true.
curl_setopt_array(
$ch, array(
CURLOPT_POST => true, //<------------ This one !
CURLOPT_URL => 'http://e4allds/',
CURLOPT_POSTFIELDS => $postData,
//OPTION1=> 'Community=SOftwareFactory/Cloud',
//OPTION2=> 'Level=V6R2015x',
CURLOPT_RETURNTRANSFER => true
));
According to manual, CURLOPT_POSTFIELDS option is The full data to post in a HTTP "POST" operation.
So, either you should switch to POST method:
curl_setopt_array(
$ch, array(
CURLOPT_URL => 'http://e4allds/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_RETURNTRANSFER => true
));
or, put all the parameters in the query-string if you wish to keep using GET method:
curl_setopt_array(
$ch, array(
CURLOPT_URL => 'http://e4allds/?' . http_build_query($postData,null,'&'),
CURLOPT_RETURNTRANSFER => true
));

Categories