post big data via Zend_Http_Client_Adapter_Curl - php

I use $site->setParameterPost and $site->request('POST')->getBody() for posting data to an action, one parameter of setParameterPost is very big data, and it doesn't send data via post method. What can I do?
$config = array('adapter' => 'Zend_Http_Client_Adapter_Curl' );
$site = new Zend_Http_Client('http://somewhere.tld/api/news', $config);
$site->setParameterPost(array(
'news' => $news, //very big data, without it data send properly
'modelName' => 'somemodel',
'method' => 'somemethod',
'key' => 'something',
'siteName' => $sitename,
));
$sitedata = $site->request('POST')->getBody();

I should use streaming requests, which are allowed only with PUT method.
$http_client = new Zend_Http_Client ('http://something.some/thing');
$http_client->setConfig (array (
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'timeout' => 180
));
$file = fopen ('news.dat', 'r'); // put all your news to the file beforehand
$http_client->setRawData ($file);
$http_client->setParameterPost (array (
'modelName' => 'somemodel',
'method' => 'somemethod',
'key' => 'something',
'siteName' => $sitename
));
$response = $http_client->request ('PUT');
On the server-side you can access your big data through
fopen ("php://input", "r");

Related

How to send file via pecl_http?

I'm writing API Client, but I can't send files via pecl_http. I wrote everything on http\Client. Most things by copy Postman things, but when I send I get null files. How should I send it? how I should put into this script my $_FILES variable with data?
<?php
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->addForm(NULL, array(
array(
'name' => 'photo',
'type' => null,
'file' => 'user_path/2018-11-09 o 15.00.48.png',
'data' => null
)
));
$request->setRequestUrl('url');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders(array(
'Postman-Token' => 'f6154fff-46f4-47d0-a7c3-98d7de8d0f24',
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/x-www-form-urlencoded'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
Here is part of the code I found in: Pecl_http Tutorial
<?php
$r = new HttpRequest('http://dev.iworks.at/.print_request.php', HTTP_METH_POST);
// if redirects is set to true, a single redirect is allowed;
// one can set any reasonable count of allowed redirects
$r->setOptions(
array( 'cookies' => array('MyCookie' => 'has a value'),
'redirect' => true,
)
);
// common form data
$r->setPostFields(
array( 'name' => 'Mike',
'mail' => 'mike#php.net',
)
);
// add the file to post (form name, file name, file type)
touch('profile.jpg');
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
print $r->send()->getBody();
} catch (HttpException $e) {
print $e;
}
?>

Facebook Ads SDK - Unsupported Post Request

I got some example scripts from Facebook App Management to use the Marketing API. When I run the script, I just get this error by curl:
'Unsupported post request. Object with ID \'105101623679981\' does not exist, cannot be loaded due to missing permissions, or does not support this operation.
I already tried to deactivate the Sandbox Mode and go public, tried many different scripts in different languages and also other keys.
Any Ideas?
This is the Script:
<?php
//Add all those Uses and the autoloader
$access_token = '<my_very_long_accessToken';
$ad_account_id = '<my_account_id>'; //<-- This is the Object in the Error Code
$app_secret = '<my_app_secret>';
$page_id = '<my_page_id>';
$app_id = '<my_app_is>';
$api = Api::init($app_id, $app_secret, $access_token);
$api->setLogger(new CurlLogger());
$fields = array(
);
$params = array(
'objective' => 'PAGE_LIKES',
'status' => 'PAUSED',
'buying_type' => 'AUCTION',
'name' => 'My Campaign',
);
$campaign = (new AdAccount($ad_account_id))->createCampaign(
$fields,
$params
);
$campaign_id = $campaign->id;
echo 'campaign_id: ' . $campaign_id . "\n\n";
$fields = array(
);
$params = array(
'status' => 'PAUSED',
'targeting' => array('geo_locations' => array('countries' => array('US'))),
'daily_budget' => '1000',
'billing_event' => 'IMPRESSIONS',
'bid_amount' => '20',
'campaign_id' => $campaign_id,
'optimization_goal' => 'PAGE_LIKES',
'promoted_object' => array('page_id' => $page_id),
'name' => 'My AdSet',
);
//...
Yeah. I got it. It has to be "act_"
So bad. The Script is really crappy. So many Errors. And it's created by facebook!

POST request in PHP is returning PHP code when file extension is used

I am sending post requests in PHP to get a boolean value from my API (so it should return wither true or false)
This is the code I am using in the file for my API. The file is called users.php
if ($_POST['type'] == "authenticateMinecraft"){
$p = new dibdibs\post(
array(
'url' => 'https://authserver.mojang.com/authenticate',
'data' => array(
'agent' => array(
'name' => 'Minecraft',
'version' => 1
),
'username' => $_POST['username'],
'password' => $_POST['password'],
'clientToken' => "33225A179D9A4E1BDA73C012C1C3CBAB8BD00326883BDBEB6FA682482E40F68D"
)
)
);
$res = $p->json();
if (isset($res["selectedProfile"])){
echo("true");
}
else{
echo("false");
}
}
This is the code I am using to reference it (I am using a class which I have put on Pastebin to actually send the request).
$params = array(
'data' => array(
'type' => 'authenticateMinecraft',
'username' => $mcuname,
'password' => $mcpasswd
),
'url' => "api/users.php"
);
$c = new dibdibs\post($params);
$r = $c->http();
var_dump($r);
Whenever I use the .php fule extension when defining url, the whole PHP code of the API page is returned, but when I remove the extension, only true or false is returned. Why is this and is it a problem that I should be aware of and I should fox?

Trouble with PHP get contents + API

I am trying to create an Adfly shortener website for my associates to use using PHP. I thought this would be simple, but I am having some trouble getting this to work
$longURL = $_GET['longurl'];
$shortURL = shortAdfly($longURL);
// Print the result
print_r($shortURL);
// Adf.ly shortener
function shortAdfly($ToConvert) {
urlencode($ToConvert);
$apiUrl = 'http://api.adf.ly/api.php?' . http_build_query([
'key' => 'My API Key',
'uid' => 'My User ID',
'advert_type' => 'int',
'domain' => 'adf.ly',
'url' => $ToConvert ]);
$short_url = file_get_contents($apiUrl);
return $short_url;
}
Building URL query strings by hand is like encoding JSON by hand; you just shouldn't do it. Use the tools provided
$apiUrl = 'http://api.adf.ly/api.php?' . http_build_query([
'key' => 'your-api-key',
'uid' => 1234567,
'advert_type' => 'int',
'domain' => 'adf.ly',
'url' => $ToConvert
]);
return file_get_contents($apiUrl);

Batch Requests and attached files with PHP SDK

I'm trying to make a batch request to post an unique photo on differents page.
For that, I wish to use Batch Post Requests to optimize the proccess.
My Code :
$facebook = new Facebook(array('appId' => myappId, secret => mysecret, 'cookie' => true, 'fileUpload' => true, 'domain' => $_SERVER['SERVER_NAME']));
$request[0] = array(
'relative_url' => 'facebookPageId1/photos'
'method' => 'post'
'body' => 'access_token=page_access_token_1&message=my_message&attached_files=' . basename($picture));
$request[1] = array(
'relative_url' =>'facebookPageId2/photos'
'method' => 'post'
'body' => 'access_token=page_access_token_2&message=my_message&attached_files=' . basename($picture));
$file[basename($picture)] = '#' . realpath($picture);
$batch = json_encode(array_values(requests));
$params = array('batch' => $batch);
$params = array_merge($params, $file);
$facebook->api('/', 'POST', $params)
Now when I am running this code I got the following output for my two requests :
'{"error":{"message":"(#324) Requires upload file","type":"OAuthException","code":324}}'
So what's the problem ?
I set fileUpload at true on my Facebook Object and I tried to post a photo on the url "pageId/photos" with a classic request and it's worked perfectly. But witch a batch request, I have always the same error.
Thanks for your help.
EDIT : Ok I get my mistake, my requests was wrong :
$request[0] = array(
'relative_url' => 'facebookPageId1/photos',
'method' => 'post',
'body' => 'access_token=page_access_token_1&message=my_message',
'attached_files' => basename($picture)
);
$request[1] = array(
'relative_url' =>'facebookPageId2/photos',
'method' => 'post',
'body' => 'access_token=page_access_token_2&message=my_message',
'attached_files' => basename($picture)
);
But now I got the following error :
{"error":{"message":"File picturename.jpg has not been attached","type":"GraphBatchException"}}
This is what i do:
$files['access_token']=$access_token;
$request[0] = array(
'relative_url' => 'facebookPageId1/photos',
'method' => 'POST',
'body' => 'message=my_message',
'attached_files' => 'file_0'
);
$files['file_0']= basename($picture);
$batchresult = $facebook->api("/?batch=".urlencode(json_encode($request)), 'POST', $files);
Facebook Batch ask you to put the files in diferent arrays, also you can put the access token in there, and you need to put it once.

Categories