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
));
Related
I am testing an api on postman. The request body should be in x-www-form-url-encoded. My requests are being passed successfully, and am able to generate a snippet which I have shared here. However, some of the parameters that am adding to the body (Amount, and phone number) will not be static when the api is employed on my site. These parameters will vary by user. I have tried to define those parameters at the top of the code as you cane see $Airtime_amount and
$Recieving_mobile, but how can I pass them to the x-www-form-url-encoded CURLOPT_POSTFIELDS in the code below? See how am trying to pass them, but without success...
In other words, I have a url encoded string from postman, but the parameters in that string are static. i would like to make them dynamic..Like get user phone number from wordpress, and insert it in the urlencoded string
//Wordpress hook to call the api begins here
add_action('hrw_withdrawal_request_notification','disburse_airtime',7);
function disburse_airtime() {
$Airtime_amount = "KES 230";
$Recieving_mobile = "+254757777777";
//Snippet generated from postman begins here
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.sandbox.africastalking.com/version1/airtime/send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'username=sandbox&recipients=%5B%7B%22phoneNumber%22%3D%3E%24Recieving_mobile%2C%22amount%22%3D%3E%24Airtime_amount%7D%5D',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 61449ca078574078a6d0eaaa01cfb751f803797c99714f74d8541a25e2a612ef',
'Accept: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
You should probably build the POSTFIELDS string using the http_build_query function.
Your existing string has what looks like a JSON string for the value of the recipients parameter, so we can build that up using arrays, then encode it when we set it in the params.
function disburse_airtime()
{
$Airtime_amount = "KES 230";
$Recieving_mobile = "+254757777777";
$recipients = [
[
'phoneNumber' => $Recieving_mobile,
'amount' => $Airtime_amount
]
];
$params = [
'username' => 'sandbox',
'recipients' => json_encode($recipients)
];
$postFields = http_build_query($params);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.sandbox.africastalking.com/version1/airtime/send',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'apiKey: 61449ca078574078a6d0eaaa01cfb751f803797c99714f74d8541a25e2a612ef',
'Accept: application/json'
],
]);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
Side note, if you want to "reverse engineer" the data that is in the encoded string in order to build it up in your own code, you can do so with using urldecode and parse_str:
$str = 'username=sandbox&recipients=%5B%7B%22phoneNumber%22%3D%3E%24Recieving_mobile%2C%22amount%22%3D%3E%24Airtime_amount%7D%5D';
parse_str(urldecode($str), $params);
print_r($params);
Result:
Array
(
[username] => sandbox
[recipients] => [{"phoneNumber"=>$Recieving_mobile,"amount"=>$Airtime_amount}]
)
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.
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
);
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
Do any of you know a way to get datas from a database of a website? I already studied and made a program that POST data using json. It gets the shipment status of a certain tracking number and if it's shipped, it will be changed to delivered. And I already done that.
Now what I'm trying to do is get all the datas, or the row of that tracking number and then encode it to JSON. But I don't know what to do.
This is how I did the first program: on getting a specific tracking number and update the status to delivered.
<?php
$url='https://sample.com.ph';
$apikey='apikey';
$method ='SendPackageStatus';
$data = array(
'package' => array(
'tracking_number' => '735898532',
'package_status' => 'delivered',
'failed_reason' => '',
'updated_at' => '2014-01-01 07:02:14'
)
);
$check=json_encode($data);
$postdata = "&data=$check&apikey=$apikey&method=$method";
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url.'/webservice/',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded',
)
)
);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Why not use mysql?... you can store json string on db (as..strings). I dont follow what you are trying to do.