How to convert curl file post from command line into PHP cURL - php

I'm trying to post files to a 3rd party API from my PHP app.
This works from the command line:
curl -F "file=#move_file.MOV" "https://upload.wistia.com?project_id=pbmcmua3ot&username=api&api_password=xxxxx_apikey_yyyyy"
But I can't get it to work using PHP's curl:
$data = array(
'username' => $username,
'api_password' => $api_password,
'file' => fopen($tmp_filename, 'r'),
'project_id' => $project_hashed_id,
);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_INFILE, fopen($tmp_filename, 'r') );
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmp_filename));
curl_setopt($ch, CURLOPT_VERBOSE, 1); //for debugging
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
I think CURLOPT_INFILE is the problem but I'm not sure. Thanks in advance for any help you can give me.
UPDATE1
$data = array(
'username' => $username,
'api_password' => $api_password,
'file' => '#'.$tmp_filename,
'project_id' => $project_hashed_id,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tmp_filename));
curl_setopt($ch, CURLOPT_VERBOSE, 1); //for debugging
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
UPDATE 2 (Working)
$data = array(
'api_password' => $api_password,
'file' => '#'.$tmp_filename,
'project_id' => $project_id
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);

You don't need to open a handle for curl, just have
$data = array(
'file' => '#move_file.MOV'
);
Curl will see the # and treat it as a file upload attempt. You also don't have to do http_build_query() either. Curl can accept an array directly and do the query building itself:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Related

error_code 129 in VK api appWidgets.saveAppImage

Good day! I'm trying to load an image using the method appWidgets.saveAppImage. In the beginning I receive URL the server for loading -> getAppImageUploadServer, there all ok!
I receive a hash and an image, send them a POST request and get an error. Here is my code:
$token = "Service_access_key";
$tmp_image = file_get_contents('https://www.ejin.ru/wp-content/uploads/2017/12/667108931864_667108931864-150x150.jpg');
file_put_contents(dirname(__FILE__).'/tmp.jpg',$tmp_image);
$img_path = dirname(__FILE__).'/tmp.jpg';
$post_data = array("image" => "#".$img_path);
$upload_url = file_get_contents("https://api.vk.com/method/appWidgets.getAppImageUploadServer?v=5.85&image_type=50x50&access_token=".$token);
$url = json_decode($upload_url)->response->upload_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = json_decode(curl_exec($ch),true);
$safe = file_get_contents("https://api.vk.com/method/appWidgets.saveAppImage?v=5.85&hash=".$result['hash']."&image=".$result['image']."&access_token=".$token);
echo $safe;
echo:
"error_code":129,"error_msg":"Invalid photo: file not found, from upl_850128?act=app_widget_image"
what's my mistake?
<?
$request_params = array(
'image_type' => '510x128',
'access_token' => 'xxx',
'v' => '5.92'
);
$t = json_decode(file_get_contents('https://api.vk.com/method/appWidgets.getAppImageUploadServer?'. http_build_query($request_params)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $t->response->upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("image" => new CURLFile(dirname(__FILE__).'\img.jpg')));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = json_decode(curl_exec($ch));
curl_close($ch);
$request_params = array(
'hash' => $result->hash,
'image' => $result->image,
'access_token' => 'xxx',
'v' => '5.92'
);
print_r(file_get_contents('https://api.vk.com/method/appWidgets.saveAppImage?'. http_build_query($request_params)));
?>

Getting "bool(false)" from MailChimp API 3 POST request

All,
I'm trying to do a simple "post" to Mailchimp using the 3.0 API; however I'm just getting a bool(false) response from the below code. I know the MAILCHIMP_API_KEY and LIST_ID variables are correct... Help?
All I want to do is add an email & first name to a specific list.
$auth = base64_encode( 'user:'.MAILCHIMP_API_KEY);
$data = array(
'apikey' => MAILCHIMP_API_KEY,
'email' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.NEW_LIST_ID.'/members/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
var_dump($result);
die('<br /><br />Mailchimp executed');
Just missed the dot after $server in the URL, and also should be email_address rather than email. But below is a working example if anyone needs it:
$auth = base64_encode( 'user:'.MAILCHIMP_API_KEY);
$data = array(
'apikey' => MAILCHIMP_API_KEY,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'.api.mailchimp.com/3.0/lists/'.NEW_LIST_ID.'/members');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
die('<br /><br />Mailchimp executed');

How to upload a file or folder in box using curl php.?

I want to upload an image in to my box account to the root folder of it.
How can i achieve this.Below is my code,Please checkit.
https://upload.box.com/api/2.0/files/content
code
$json = json_encode(array(
'name' => 'spring.jpg',
'folder' => array('id' =>'0')// 0 - uploads to main folder
));
$fields = array(
'attributes' => $json,
'file'=> new CURLFile('#C:\xampp\htdocs\box\spring.jpg')
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Bearer W1UkcTHsuuiDCA3Ej5n4xwMqoSGh0iVW'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
var_dump($fields);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);

LinkedIn API file_get_contents timing out

I currently use file_get_contents() to call the LinkedIn Authentication API.
I successful call /uas/oauth2/authorization but when I call /uas/oauth2/accessToken with file_get_contents() it times out.
The odd thing is that it works perfectly on my localhost.
I've made sure allow_url_fopen is on and manage to open google.com with file_get_contents().
As you can probably imagine, it's driving me crazy trying to debug it (and fix it).
Do any of you have any suggestions on why this is the case?
The issue is because /uas/oauth2/accessToken requires a POST type method, file_get_contents always uses GET. Consider switching to curl, method for both of your calls are provided below.
This information is available within the documentation
Variables for both calls
$apiKey = '';
$state = '';
$scope = '';
$redirectUri = '';
/uas/oauth2/authorization
$postData = http_build_query(
[
'response_type' => 'code',
'client_id' => $apiKey,
'scope' => $scope
'state' => $state,
'redirect_uri' => $redirectUri
]
);
$ch = curl_init();
$endpoint = sprintf('%s?%s', 'https://www.linkedin.com/uas/oauth2/authorization', $postData);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
/uas/oauth2/accessToken
$postData = http_build_query(
[
'grant_type' => 'authorization_code',
'client_id' => $apiKey,
'scope' => $scope
'state' => $state,
'redirect_uri' => $redirectUri
]
);
$ch = curl_init();
$endpoint = 'https://www.linkedin.com/uas/oauth2/accessToken';
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);

how to delete photo post id facebook by grab api php with curl

i try to delete photos i have uploaded with app by php
the result of upload example :
{"id":"429774393794352","post_id":"276744849097308_429774413794350"}
first which one id or post_id will be use
and how to select it with php ??
and how to make order to delete it with php code by curl library
my idea is , i will store all photos ids into sql to delete it any time
my upload code i used
$file='./'.$new_string;
$args = array(
'message' => $message,
);
$args[basename($file)] = '#' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/album id /photos?access_token='.$accsesss;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
i try
$args = array(
'id' => '276744849097308_429774413794350',
'access_token' => $accsesss ,
);
$ch = curl_init();
$url = 'https://graph.facebook.com/276744849097308_429774413794350?method=DELETE&access_token=$accsesss';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); $a1=$data = curl_exec($ch);
print_r(json_decode($data,true));
echo'<br/>';
result
Array ( [error] => Array ( [message] => Invalid OAuth access token. [type] => OAuthException [code] => 190 ) )
$Curl_Session = curl_init('https://graph.facebook.com/429774393794352');
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "method=DELETE&access_token=$masteracsess");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, 1);
echo $a8=curl_exec ($Curl_Session);
curl_close ($Curl_Session);
thanks for your help #CBroe

Categories