I created a PHP-script which is uploading an image, picked up from an HTML-form, to the Parse.com backend.
Actually it looks like the script is working, because I am getting a URL and a name as result.
After the Upload I am associating the uploaded file with an Object.
The result is also looking fine and the image is appearing inside the data browse.
But if I try to get the image in my iOS app or take a look at it inside of the browser (by clicking on it) I only get an access denied alert or a white page (with broken image icon).
iOS Error: Error Domain=Parse Code=150 "The operation couldn’t be completed. (Parse error 150.)
Here you can see my code:
Upload image:
$teamImage = $_FILES["teamImage"];
$APPLICATION_ID = "XXXXXXXXXXXXXXXXXXX";
$REST_API_KEY = "XXXXXXXXXXXXXXXXXXX";
$urlFile = 'https://api.parse.com/1/files/' . $teamImage['name'];
$image = $teamImage['tmp_name'];
$headerFile = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: ' . $teamImage['type'],
'Content-Length: ' . strlen($image),
);
$curlFile = curl_init($urlFile);
curl_setopt($curlFile, CURLOPT_POST, 1);
curl_setopt($curlFile, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlFile, CURLOPT_POSTFIELDS, $image);
curl_setopt($curlFile, CURLOPT_HTTPHEADER, $headerFile);
curl_setopt($curlFile, CURLOPT_SSL_VERIFYPEER, false);
$responseFile = curl_exec($curlFile);
$httpCodeFile = curl_getinfo($curlFile, CURLINFO_HTTP_CODE);
$result = array('code'=>$httpCodeFile, 'response'=>$responseFile);
Associating image to Object (image name for test case hardcoded)
$url = 'https://api.parse.com/1/classes/Teams';
$data = array(
'name' => 'Test',
'teamImage' => array(
'name' => '......jpg',
'__type' => 'File'
),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen($_data),
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$responseTwo = curl_exec($curl);
$httpCodeTwo = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$resultTwo = array('code'=>$httpCodeTwo, 'response'=>$responseTwo);
Image Url, which I am getting back from Parse: http://files.parse.com/725d8f61-de18-4de5-a84c-dcc6e74c43ae/197e7bb6-62ad-4dc4-a011-6db88333ac45-BMW_1series_3door_Wallpaper_1920x1200_01.jpg
Data Browser Screenshot:
I think you need to specify the url as well
$data = array(
'name' => 'Test',
'teamImage' => array(
'name' => '......jpg',
'__type' => 'File',
'url' => '....jpg'
),
);
Related
How do I insert json post data with curl and api key parameter.
In the following code I'm trying to add an apikey parameter in the url.
index.php:
$data = array(
// 'name' => '100236',
// 'age' => '265',
'nric' => '123546',
'id' => '266',
'fullname'=>'sairam',
'gender' => 'M',
'password' => '123546',
'address'=>'jlnklmanalpoi',
'postcode' => '502103',
'state' => 'telangana',
'contact1'=>'+60123654',
'email' => 'email#email.com',
'rank'=>'AM',
'expirydate'=>'14-07-2017'
);
$post_json = json_encode($data);
$api_key = '99999your9999api999keyd4eae7c3';
$endpoint = 'http://localhost/apib/data.php?key=.$api_key ';
$ch = #curl_init();
#curl_setopt($ch, CURLOPT_POST, true);
#curl_setopt($ch, CURLOPT_POSTFIELDS, $post_json);
#curl_setopt($ch, CURLOPT_URL, $endpoint);
#curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = #curl_exec($ch);
$status_code = #curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errors = curl_error($ch);
#curl_close($ch);
echo "curl Errors: " . $curl_errors;
echo "\nStatus code: " . $status_code;
echo "\nResponse: " . $response;
?>
am unable to use api key below page :
this is end point data.php
$_POST = json_decode(file_get_contents('php://input'), true);
if (isset($_POST['nric'],$_POST['id'],$_POST['fullname'],$_POST['gender'],$_POST['password'],$_POST['address'],$_POST['postcode'],$_POST['state'],$_POST['contact1'],$_POST['email'],$_POST['rank'],$_POST['expirydate'])) {
global $db;
$db = mysqli_connect("localhost", "root", "", "ilde");
if($db === false)
{
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$nric=mysqli_real_escape_string($db,$_POST["nric"]);
$id =(int)$_POST["id"];
$fullname=mysqli_real_escape_string($db,$_POST["fullname"]);
$gender=mysqli_real_escape_string($db,$_POST["gender"]);
i want to use secure url with apikey manually.please help me to solve this. how to insert json post data with curl and api key parameter.please help me below is my code i am trying to add apikey parameter in url
Try below endpoint:
$endpoint = 'http://localhost/apib/data.php?key='.$api_key.' ';
I am trying to retrieve data from salesforce using the REST api and CURL in PHP.
I perform the authentication request and recieve an 'instance_url' and 'access_token' but after performing a query request using those, i receive an "INVALID_SESSION_ID" error.
my authentication request code:
function get_sf_auth_data() {
$post_data = array(
'grant_type' => 'password',
'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', //My client id (xxx... for this example)
'client_secret' => '111111111111', // My client secret (111... for this example)
'username' => 'my_user_name',
'password' => 'my_user_password'
);
$headers = array(
'Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'
);
$curl = curl_init('https://login.salesforce.com/services/oauth2/token');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
curl_close($curl);
// Retrieve and parse response body
$sf_access_data = json_decode($response, true);
echo '*********' . $sf_response_data['instance_url'] . '********';
echo '*********' . $sf_response_data['access_token'] . '********';
return $sf_access_data;
}
My query request code:
function get_user_sfid($sf_access_data, $user_id_number){
$sql = "SELECT Id, Name
FROM Account
WHERE ID__c = '$user_id_number'";
$url = $sf_access_data['instance_url'] . '/services/data/v20.0/query/?q=' . urlencode($sql);
$headers = array(
'Authorization' => 'OAuth ' . $sf_access_data['access_token']
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$json_response = curl_exec($curl);
curl_close($curl);
var_dump($json_response); // This prints out the response where i got the error
$response = json_decode($json_response);
return $response['id'];
}
The response to the query request as is:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I have also tried using this guide (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) as reference but it uses an "authorization_code" authentication and not a password one.
EDIT
The app has full access in salesforce and both api version and authentication data are correct
I found the solution.
I defined my headers array as:
$headers = array(
"Authorization" => "OAuth " . $sf_access_data['access_token']
);
When i should have defined it as:
$headers = array(
"Authorization: OAuth " . $sf_access_data['access_token']
);
I want to push notification with below code
the json I want to get is something like this
{
"data": {
"message": "Hello! Welcome to parse notifications.",
"title": "AndroidHive"
},
"is_background": false
}
below is php code
<?php
$url = 'https://api.parse.com/1/push';
$APPLICATION_ID = "T7G0qlJG5ygBVHZFVrqf8VO17vhQeeG3TnshxfQy";
$REST_API_KEY = "ywsoZIXPlrapASLHu5kvNBdCl7kfPe43OW3ugyZd";
$url = 'https://api.parse.com/1/push';
$data = array(
'channles'=>'',
'data'=>array('data'=>array(
'message'=>'Hello! Welcome to parse notifications.',
'title'=> 'ParseTest',
),
"is_background"=> false
),
);
$_data = json_encode($data);
$headers = array(
'X-Parse-Application-Id: ' . $APPLICATION_ID,
'X-Parse-REST-API-Key: ' . $REST_API_KEY,
'Content-Type: application/json',
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_exec($curl);
?>
But nothing send to my device, anyone know how to solve it? Thank you.
The problem is the curl,add such code with make the curl work fine.
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER ,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST ,false);
I'm the using MailChimp 2.0 api and trying to post a lists/subscribe call using php. The call is returning an error "You must specify a apikey value".
Here's the code that makes the post:
function json_post ($url, $params)
{
print '<p>url = ' . $url . '</p>';
$data = json_encode ($params);
print '<p>data = ' . $data . '</p>';
$handle = curl_init ($url);
curl_setopt ($handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt ($handle, CURLOPT_POST_FIELDS, $data);
curl_setopt ($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($handle, CURLOPT_HTTPHEADER, array ('Content-Type: application/json',
'Content-Length: ' . strlen($data_string)));
$result = curl_exec ($handle);
print '<p>curl_error: ' . curl_errno ($handle) . '</p>';
return $result;
}
The print statements show:
url = https://us10.api.mailchimp.com/2.0/lists/subscribe.json
data = {"apikey":"...","id":"...","email":{"email":"test1#abc.com"},"merge_vars":{"groupings":{"name":"test"}}}
curl_error: 0
{"status":"error","code":-100,"name":"ValidationError","error":"You must specify a apikey value"}
I presume there's something wrong with the syntax. The api key is cut & pasted from my mailchimp account page. I've tried it with and without the -us10 suffix. Any ideas?
To subscribe:
$email='';
$apikey='';
$listId='';
$data = array(
'email_address'=>$email,
'apikey'=>$apikey,
'merge_vars' => array(),
'id' => $listId,
'double_optin' => false,
'update_existing' => true,
'replace_interests' => false,
'send_welcome' => false,
'email_type' => 'html'
);
$submit_url = "http://us6.api.mailchimp.com/1.3/?method=listSubscribe";
$payload = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $submit_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($payload));
$result = curl_exec($ch);
curl_close ($ch);
$data = json_decode($result);
if (isset($data->error) and $data->error){
//Error
} else {
//Ok
}
Cases where you get "API Key Missing" but the API Key is definitely there usually comes from JSON Syntax errors, which MailChimp doesn't catch specifically. You'll want to make sure that JSON isn't getting double-encoded or anything like that.
In this case, it is probably CURLOPT_POST_FIELDS -- the actual PHP Constant you're looking for is CURLOPT_POSTFIELDS.
You should use Guzzle or another HTTP library to ensure you're not double-encoding your JSON or otherwise getting tripped up by the Curl library's verbosity.
I have a little problem with my FB application. It's about that I always get error:
{"error":{"message":"(#353) Missing video file","type":"OAuthException","code":353}}
with this code:
$post_url = "https://graph-video.facebook.com/xxx/videos?"
. "title=" . $video_title . "&description=" . $video_desc
. "&access_token=" . $access_token;
$ch = curl_init();
$data = array(
'source' => 'http://x/upload/' . $name . '.' . $type,
'file' => './upload/' . $name . '.' . $type,
'type' => 'avi',
);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
if (!$response)
{
print_r(Debug::vars(array(curl_error($ch), curl_errno($ch))));
}
curl_close($ch);
File exist, access_token is valid and is logged as a app, In $data I've tried set only 'file' or 'source' but effects was the same.
You are best off using the Facebook PHP SDK for this, but it might be as simple as removing your file param and adding "#" to your source path. Here is a working example with the SDK (assuming you have asked for the appropriate FB permissions):
$this->facebook->setFileUploadSupport(true);
$upload = $this->facebook->api('/me/videos', 'POST', array(
'source' => '#'.$file,
'title' => $title,
'description' => $description
));