Directly upload a video to youtube by pure PHP and OAuth - php

I just want to send a POST request to YouTube's server to upload a video directly to YouTube via OAuth.
I have an access token and developer key - how could I send a POST request from PHP to YouTube's gdata server?
The POST request I want to send is found here: Sending an Upload API Request
POST /feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY
Slug: VIDEO_FILENAME
Content-Type: multipart/related; boundary="BOUNDARY_STRING"
Content-Length: CONTENT_LENGTH
Connection: close
--<boundary_string>
Content-Type: application/atom+xml; charset=UTF-8
API_XML_request
--BOUNDARY_STRING
Content-Type: VIDEO_CONTENT_TYPE
Content-Transfer-Encoding: binary
<Binary File Data>
--BOUNDARY_STRING--
I tried the below function, and a lot more like it, but it can't send the XML part of the above mentioned POST request.
if (!function_exists('send_post_request'))
{
function send_post_request($url, $data)
{
$context = stream_context_create(array('http' => array('method' => 'POST',
'content' => http_build_query($data))));
$result = file_get_contents($url, FALSE, $context);
return $result;
}
}
I also tried cURL!

Related

Docusign PHP SDK resending envelope with Demo Account updates the envelope but no email is sent

I am trying to resend an existing envelope that a user may have mislaid or otherwise not received. The API is updating the envelope with the request, but is not resending the email. I am receiving a 200 OK response from the api. This is my call;
$envelopeApi->update($account_id, $env_id, json_encode([resend_envelope => true]));
The logs show the call is successful;
PUT https://demo.docusign.net:7801/restapi/v2/accounts/eb84945a-xxxx-xxxx-xxxx-125dae50be01/envelopes/1e748673-xxxx-xxxx-xxxx-d932f6bdb90e
Content-Type: application/json
Content-Length: 24
Transfer-Encoding: chunked
Accept: application/json
Authorization: Bearer [omitted]
Host: demo.docusign.net
User-Agent: Swagger-Codegen/2.0.1/php
X-DocuSign-SDK: PHP
X-SecurityProtocol-Version: TLSv1.2
X-SecurityProtocol-CipherSuite: ECDHE-RSA-AES256-GCM-SHA384
x-forwarded-for:
{"resend_envelope":true}
200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 60
X-DocuSign-TraceToken: 3f4d4386-xxxx-xxxx-xxxx-ede4199f7f35
{
"envelopeId": "1e748673-xxxx-xxxx-xxxx-d932f6bdb90e"
}
I have read all the threads I can find on SO and none seem to cover my experience. Your help would be appreciated.
For anyone stumbling across this post, this is how to resend an envelope using v2 of the PHP SDK, as provided by dev support. Its a shame the SDK isn't better documented.
$options = new DocuSign\eSign\Api\EnvelopesApi\UpdateOptions();
$options ->setResendEnvelope("True");
$results = $envelopeApi->update(self::$accountID, $envelopeid, "{}", $options);
Thanks to Edwin#DS

Onedrive uploading fails with the Internal Server Error

I'm trying to create file on the OneDrive using REST API with PHP, but in the response I retrieve HTTP status code 500.
Code:
`
$url = $this->buildUrl(
'{folder_id}/files/{filename}?access_token={token}',
array(
'folder_id' => $folderId,
'filename' => $filename,
'token' => $this->getAccessToken(),
)
);
$response = wp_remote_request($url, array(
'body' => $content,
'method' => 'PUT',
));
`
Error message from the response body:
An error occurred while performing the action. Try again later.
What i'm doing wrong?
I just went through the same problem. It worked for me when I removed 'Content-type' line from request header.
If you are using PHP Curl to send request in wp_remote_request, you can remove 'Content-type' line from request header by calling something similar to this, before calling curl_exec:
curl_setopt($ci, CURLOPT_HTTPHEADER, array("Content-Type:"));
By adding the code above, the actual request header looks like this (note there is no 'Content-Type'):
PUT /v5.0/{folderId}/files/{filename}?access_token={accesstoken}
User-Agent: SOMEAGENT
Host: apis.live.net
Accept: */*
Expect: 100-continue
Content-Length: 29
FYI: I got a hint from here:
http://msdn.microsoft.com/en-us/library/dn631834.aspx
"For a PUT request, leave the Content-Type blank and put the contents of the file in the request body."
Hope it helps.

POST request with files and fields in guzzle

Ok I'll give a little background to start. I have system A written in CakePHP that handles ads and products and such. Recently I have been working on another system, written in Laravel, that acts as a self-serve tool for realtors to post and manage their real estate listings that reside in system A. I am now at the point of uploading images from the self-serve site to system A. I wrote a simple controller action in Cake to handle a POST request and save the image file on the server.
http://example.com/image/add
I am able to send POST requests, upload images and get a proper response using REST applications such as postman. All looks good on system A (Cake) side of things.
Now in the self-serve system, in Laravel, I am using Guzzle to send HTTP requests. I have filled out the guzzle post request with the exact same fields and files, but I don't recieve the same output. The request is received by system A, but the image is not added and a random HTML page is returned. If postman and a few other applications get the exact same response and functionality, but my request sent in Guzzle is not, I am thinking there is an issue with my guzzle request. Here is my guzzle code:
$client = new Client();
// Create the request.
$request = $client->createRequest("POST", "http://example.com/image/add");
// Set the POST information.
$postBody = $request->getBody();
$postBody->setField('user_id', $userId);
$postBody->setField('api_key', $token);
$postBody->setField('product_id', $product_id);
$postBody->addFile(new PostFile('image[data]', fopen('tmp/images/'.$input->image, 'r')));
// Send the request and get the response.
$response = $client->send($request);
Here is the working POST request from Postman:
POST /image/add HTTP/1.1
Host: example.com
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="product_id"
1000
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="user_id"
8345
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="api_key"
secretKey
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="data[image]"; filename="steve-jobs.jpg"
Content-Type: image/jpeg
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Here is the guzzle request to string, I apologize for readability issues.
POST /image/add HTTP/1.1 Host: example.com User-Agent: Guzzle/4.0.2 curl/7.22.0 PHP/5.5.9-1+sury.org~precise+1 Content-Length: 124647 Content-Type: multipart/form-data; boundary=53a1f21e04080 --53a1f21e04080 Content-Disposition: form-data; name="user_id" 8345 --53a1f21e04080 Content-Disposition: form-data; name="api_key" secretKey --53a1f21e04080 Content-Disposition: form-data; name="product_id" 1000 --53a1f21e04080 Content-Disposition: form-data; filename="steve-jobs.jpg"; name="image[data]" Content-Type: image/jpeg
Then a bunch a characters for the image data.
I am asking if anyone can see an issue with my POST request in guzzle or if anyone has run into this kind of weird issue with guzzle before.
Edit:
I am using CakePHP 2.4.1
As far as I can tell it's just a small mistake in the file field name, it should be data[image], not image[data].
new PostFile('data[image]', /* ... */);
In your app you are probably relying on the file data being available via CakeRequest::$data, however it will only land there in case it's wrapped in the data key, otherwise it is added to CakeRequest::$params['form'].
Not really a duplicate, however for (a more CakePHP form helper related) reference see also:
CakePHP: posted file data not included in request->data

PayPal API with Zend Framework

Hi I want to integrate Paypal API in my Zend FW 1 application.
my code is
$this->setHeaders(
array(
'Authorization' => '<REMOVED>',
'content-type' => 'application/json',
)
);
$this->setMethod('POST');
$this->setParameterGet('payer_id', $company_id);
$this->setParameterGet('number', $cc_number);
$this->setParameterGet('type', $type);
$this->setParameterGet('exp_month', $exp_month);
$this->setParameterGet('exp_year', $exp_year);
$this->setParameterGet('payer_id', $company_id);
$this->setParameterGet('first_name', $first_name);
$this->setParameterGet('last_name', $last_name);
return $this->request();
Debug show this
string(364) "POST /v1/vault/credit-card?number=4417119669820331&type=visa&exp_month=05&exp_year=2019&first_name=john&last_name=travolta HTTP/1.1
Host: api.sandbox.paypal.com
Connection: close
Accept-encoding: gzip, deflate
User-Agent: Zend_Http_Client
Authorization: Bearer <REMOVED>
content-type: application/json
Content-Length: 0
But when I run i get this error from PayPal
"Method Not Allowed"
Where is problem? I do POST and he said that is not allowed? Allowed method are POST, GET, HEAD, OPTIONS. Where i making mistakes?
I want to store credit card
Paypal API requires strict usage of correct requests.
This includes a valid Accept: application/json header and valid
json content being send. You can not set content-type without sending any content
In this case Zend_HTTP_Client's rawData() should be used.

Google OpenSocial API issue about Curl Method

I am trying to build an application using Google Opensocial API.
But when i make a POST request with following CURL headers, i am getting 405 Method not allowed error :
CURLOPT_URL : http://www.orkut.com/social/rpc/
CURLOPT_POSTFIELDS : [{"method":"people.get","id":"self","params":{"userId":["#me"],"groupId":"#self","fields":["displayName","currentLocation","thumbnailUrl","gender","name"]}},{"method":"people.get","id":"friends","params":{"userId":["#me"],"groupId":"#friends","fields":["displayName","currentLocation","thumbnailUrl","gender","name"],"count":300}}]
CURLOPT_CUSTOMREQUEST : POST
CURLOPT_RETURNTRANSFER : true
CURLOPT_USERAGENT : osapi 1.0
CURLOPT_FOLLOWLOCATION : 1
CURLOPT_SSL_VERIFYPEER : false
CURLOPT_HEADER : true
CURLINFO_HEADER_OUT : true
CURLOPT_HTTPHEADER : Array
(
[0] => Authorization: OAuth oauth_body_hash="A3ZOHT4b3YMOzEfg+2j3v+N302E=", oauth_nonce="6ad533sdfsb9261ssdfsdf29af7d", oauth_version="1.0", oauth_timestamp="1318236734", oauth_consumer_key="www.mydomain.com", oauth_token="1%2FKhAasdfsd8YX2bfsdfsdf6MsdfsfsdfsdfJYyULWUog", oauth_signature_method="HMAC-SHA1", oauth_signature="osdf4pShOsdfsdf88nnpwsdfsdf9g%3D"
[1] => Content-Type: application/json
[2] => Content-Length: 0
[3] => User-Agent: osapi 1.0
)
Following is the CURL-POST response I am recieving :
["http_code"] => int(405)
["data"] => string(12614)
"HTTP/1.1 405 Method Not Allowed
Content-Type: text/html; charset=UTF-8
Content-Length: 12462
Date: Tue, 14 Jun 2011 08:07:58 GMT
Server: GFE/2.0
I asked about this issue on Google Codes forum and they told me to remove following line from CURL request as my website is hosted on hostgator.
CURLOPT_CUSTOMREQUEST : POST
I tried above thing but still i am getting 405 Method not allowed error. Kindly assist me further on this issue.
Do i need to modify my PHP-CURL code above to send POST request? If so, then please help me..
The Google Social specification has an example in which people.get is requested with GET, not via POST. Generally keep in mind that GET is for requesting information, while POST is for changing existing information.

Categories