How to upload file via cURL in kohana 3? - file-upload

I try
$request = Request::factory($url)->method(Request::POST)->post('xml', '#' . $filepath);
echo $request->execute();
but print_r($_FILES); in destination script returns empty array.
Version of Kohana is 3.2.0 stable
What I want is a simple analog of
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('xml' => '#' . $filepath));
$res = curl_exec($ch);
echo $res;

Try:
$request_url = 'your_url_here';
$post_params['name'] = urlencode('Test User');
$post_params['file'] = ‘#’.'demo/testfile.txt’;
$post_params['submit'] = urlencode('submit');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
curl_close($ch);

Now it's possible to do that with Kohana 3.3 using Request#files() method.
Edit: actually, it's not part of official release, so before 3.4 you probably won't be able to do it.

Related

How to round the degrees OpenWeather APIs

I have a script to extract the time, and for a long time I've been trying to round the degrees from 6.27C ° to 6C °. How can I do this?
<?php
$apiKey = "";
$cityId = "";
$apiUrl = "http://api.openweathermap.org/data/2.5/weather?id=" . $cityId . "&lang=en&units=metric&APPID=" . $apiKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$currentTime = time();
?>
<span class="weather-temp"><?php echo $data->main->temp;?> C°</span>

Find out URL response to check broken Url's in Codeception

I have fetched multiple URL's using grabMultiple() method.
I want to check its response to check whether it is broken(i.e.404) or not.
Can I use HTTP Response in Codeception ? If yes what is the syntax ?
Use seeResponseCodeIs method.
$links = $I->grabMultiple('div.test-info a', 'href');
foreach ($links as $link) {
$I->amOnPage($link);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
}
This worked for me -
function check_url($link){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if($code==404)
{
echo "Bad url";
}
else{
echo "Good url";
}
}

Updating Event Details using Eventrite API v3

I'm trying to edit Eventbrite event using version 3 of the API but I always returns false.
$request_url = 'https://www.eventbriteapi.com/v3/events/xxxxxx';
$options = array(
'http' => array(
'method' => 'POST',
'header'=> "Authorization: Bearer xxxxxxxxxx"
),
'name' => array(
'text'=>'Hi',
'html'=>'<p>Hi</p>'
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
var_dump($json_data);
This always returns me :
boolean false
What am I doing wrong?
You don't need to create an options array with curl as you are setting those options in curl_setopt. Just make a data array and submit it.
<?php
$request_url = 'https://www.eventbriteapi.com/v3/events/xxxxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'name' => array(
'text'=>'Hi',
'html'=>'<p>Hi</p>'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$json_data = curl_exec($ch);
$resp_info = curl_getinfo($ch);
curl_close($ch);
var_dump($json_data);
Though according to the docs there is a minimum amount of data to create an event.

shorte.st new api convert to php curl

shorte.st is a url shorten service.
recently they changed the api as following:
curl commandline
curl H "public-api-token: fakekey" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url
response:
{"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}
How to change it into php curl version?
function shst($url){
$apiurl="https://api.shorte.st/v1/data/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $apiurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('public-api-token: fakekey','X-HTTP-Method-Override: PUT'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "urlToShorten=".$url);
$data = curl_exec($ch);
curl_close($ch);
$obj = json_decode($data);
$ret=$obj->{'shortenedUrl'};
return $ret;
}
The sample they provided used the wrong url. should be https not http
This works:
function shst($url){
$key="";//your key
$curl_url = "https://api.shorte.st/s/".$key."/".$url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$array = json_decode($result);
$shortest = $array->shortenedUrl;
return $shortest;
}

PayPal OAuth2 REST API Basics

Im trying out the new PayPal REST API and Im wanting to learn how to use it using PHP / curl.
I am new to using curl so please forgive...
From the paypal developers documentation I gather the required fields and I put together the following:
$ch = curl_init();
$postDataArray = array("grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postDataArray);
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');
$headerArray = array('Accept: application/json','Accept-Language: en_US');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$clientID_Secret = "clientID:secret";
curl_setopt($ch, CURLOPT_USERPWD, $clientID_Secret);
the next few lines i added due to googling.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
print_r($result);
This returns nothing.
Please help.
here is a sample: https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php
copying the curl specific code:
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
Your code is working. But you have to change this $postDataArray = array("grant_type=client_credentials"); to $postDataArray = "grant_type=client_credentials";. Need to remove the array type;