PHP Curl File Upload Multipart Boundary - php

Writing an application that proxies a file upload. I'm using CURL to post the file but having a few problems. Posting to the script is ok, its posting from the script to the next server which is the problem. I keep getting this error from the server:
"the request was rejected because no multipart boundary was found"
here is my code:
$post = $_POST;
// allow for file upload proxying
if( !empty( $_FILES ) ){
// add to post data
foreach( $_FILES as $name => $upload ){
$post[ $name ] = '#' . $upload[ 'tmp_name' ] . ';type=image/png';
}
}
// init curl
$ch = curl_init( $url );
// configure options
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
// post data
if( !empty( $post ) ){
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post );
// for file uploads, multi-part
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-type: multipart/form-data;'
) );
}
// execute and get return value
$return = trim( curl_exec( $ch ) );
// cleanup
curl_close( $ch );
unset( $ch );
Everything I've read online suggests that this should work and also that setting the header content type is unnecessary, but when I remove the content type I get this error:
"the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null"
any ideas? thanks in advance

Not sure exactly what the problem was, but I was working locally when I posted this. Moved the code onto a live server and the problem disappeared...

Related

Upload Media to WordPress using REST API

how to upload media-image or featured-image in wordpress using rest api.
I've created new post using WordPress REST API, and now I am uploading Image to my Wordpress Site using REST API but i am unable to achieve it due to error "No data Supplied" kindly please Check Screenshot
which is the best way to create new post in WordPress with Featured Image, right now in my mind
Upload Media File to WordPress Site and Get Media ID
Create New Post with Media ID
you almost right, just lack media(image) raw binary data.
for python, using code:
import requests
toUploadImagePath = "/xxx/xxx.jpg"
mediaImageBytes = open(toUploadImagePath, 'rb').read()
# b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\.....'
uploadImageFilename = "661b943654f54bd4b2711264eb275e1b.jpg"
curHeaders = {
"Authorization": "Bearer xxx.yyy.zzz-xxx-yyy-zzz",
"Content-Type": "image/jpeg",
"Accept": "application/json",
'Content-Disposition': "attachment; filename=%s" % uploadImageFilename,
}
resp = requests.post(
"https://www.crifan.com/wp-json/wp/v2/media",
headers=curHeaders,
data=mediaBytes,
)
full code can refer my lib: crifanWordpress.py
and my Chinese post: 【已解决】用Python通过WordPress的REST API上传图片
(will publish in short future)
Using the REST API to upload a file to WordPress is quite simple. All you need is to send the file in a POST-Request to the wp/v2/media route.
UPDATED added data response true
$file = file_get_contents( 'test.jpg' );
$url = 'http://example.com/wp-json/wp/v2/media/';
$ch = curl_init();
$username = 'admin';
$password = 'password';
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, [
'Content-Disposition: form-data; filename="example.jpg"',
'Authorization: Basic ' . base64_encode( $username . ':' . $password ),
] );
$result = curl_exec( $ch );
curl_close( $ch );
print_r( json_decode( $result ) );
MORE https://gist.github.com/ahmadawais/0ccb8a32ea795ffac4adfae84797c19a

PHP Request failed on SSL

Hello I would like to get some data from a url. I already tried to make a request through the console, postman, browset - all of them worked correctly. But if I make a request using php (guzzle, symfony http client) it fails on SSL. Does anybody know how to get response from this url by curl? Thanks!
Please try the following code ( That's worked for me ):
<?php
function curl( $url, $data = array(), $headers = array(), $ssl_required = false ) {
$handle = curl_init( $url );
curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
// Set post data if exist
if ( !empty( $data ) ) {
curl_setopt( $handle, CURLOPT_POST, true );
curl_setopt( $handle, CURLOPT_POSTFIELDS, $data );
}
// Set custom headers if exist
if ( count( $headers ) )
curl_setopt( $handle, CURLOPT_HTTPHEADER, $headers );
// If url was ssl, need to true
if ( $ssl_required )
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec( $handle );
curl_close( $handle );
return $output;
}
echo curl("https://www.skroutz.gr/c/900/fakoi-epafhs.json");

PHP get request returning false

$data = file_get_contents("http://randomword.setgetgo.com/get.php");
var_dump($data);
I keep getting false when sending this get request, anyone have an idea why?
It works just fine with a simple php script I wrote hosted from the same domain, might that be the issue, how do I go about sending a get request to this API if that is the case?
I tried using curl as well with the same result. It works with my test script but not the API.
As noted in the various comments above, the code originally posted works fine for me but not for the OP - most likely due to a restriction placed upon various standard PHP functions by the webhost. As an alternative, cURL should be able to retrieve the content unless a similar restriction has been placed on standard curl functions too.
$url='http://randomword.setgetgo.com/get.php';
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_USERAGENT, 'curl-wordfetcher' );
$result = curl_exec( $ch );
if( curl_errno( $ch ) ) echo 'Curl error: ' . curl_error( $ch );
curl_close( $ch );
print_r( $result );

Google Cloud Messaging Push Notifications only work once (PHP, Android)

I am having trouble getting Google Cloud Messaging for Android working.
I have had it working before but it decides to stop working after the first couple of times.
In order to get it working again I have to delete my API key and recreate it.
I am using PHP with a SERVER API key with a IP whitelist of ::/0 (All IPv6 apparently)
Note: My android app requests a device message key each time the app is opened (Usually returns the same message key)
The Error I get is: Unauthorized Error 401
When I got to the following url to check my app message id i get 'invalid token'.
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=APA91bHuQEWGsvlRUhlSztNpqLVOZQGZPiGFHjQw2plcF-z8t29zvNNgNoDiRe-CbY9Fb-XcPQAFqJvy4HBfWTrTPPpzcY3pd5vX38WGalOsZ5iDiJeglpafLTC7eFkN4UA9JPKWZ4lqNiGLoH3w8W_GpFAFW5F-kLLzcbrPxwSFqyfUpmM8-14
The PHP code I am using is:
$data = array( 'message' => 'Hello World!222!' );
$ids = array('APA91bHuQEWGsvlRUhlSztNpqLVOZQGZPiGFHjQw2plcF-z8t29zvNNgNoDiRe-CbY9Fb-XcPQAFqJvy4HBfWTrTPPpzcY3pd5vX38WGalOsZ5iDiJeglpafLTC7eFkN4UA9JPKWZ4lqNiGLoH3w8W_GpFAFW5F-kLLzcbrPxwSFqyfUpmM8-14');
$apiKey = 'AIzaSyATkp_UTZh....'; //obviously the complete key is used...
$url = 'https://android.googleapis.com/gcm/send';
$post = array('registration_ids' => $ids, 'data' => $data);
$headers = array( 'Authorization: key=' . $apiKey, 'Content-Type: application/json');
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
$result = curl_exec( $ch );
if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}
curl_close( $ch );
echo $result;
Thanks for any help given.
EDIT: It seems to work since I unregistered my device and reregistered it with GCM. I am not sure if this is a permanent fix but it works for now.

Php schedule events

I have php script hosted in ftp server.
Its accessed from android app.
I did enabled Google Cloud Messaging in android and also php script.
So I want to send message every one hour.
So I need php schedule for trigger every one hour.
Message is constant.
My php code is:
<?php
define( "API_ACCESS_KEY", "***********************");
// Message to be sent
$message = "welcome to android app";
//RegistrationIds
$registrationid = "******************";
//call to gcm notification
send_push_notification($registrationid,$message);
//gcm push notification function
function send_push_notification($registrationids,$messages){
//GCM Implementation Code
// Set POST variables
$url = "https://android.googleapis.com/gcm/send";
$fields = array(
'registration_ids' => array($registrationids),
'data' => array( "message" => $messages ),
);
$headers = array(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
cur_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
}
?>
I want to call send push notification method every one hour.
You can use cron job for this. You dont have to add anything to the PHP script. Just running it every hour will solve the issue. Below is how you can add a cronjob. I believe you are on a Linux server.
0 * * * * /usr/bin/php /path/to/your/php/script.php
See this link to see how you can add/edit/delete cron tab - http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

Categories