Facebook Graph-API using curl - Create event with picture failed - php

I have tested the codes from this and this
but it doesn't work.
The event is created successfully but the image is not published.
Here is my actual code:
require_once '../admini/config.php';
$t = getToken('appID', 'appSecret');
$file = 'Koala.jpg';
$arrData = array(
'name' => 'Test Event',
'start_time' => '2015-07-04', //ISO-8601 format - With Time - 2012-07-04T19:00:00-0700
//'end_time' => '', //optional
'description' => 'Das erste Test-Event',
'location' => 'Deggendorf', //Just a name
'location_id' => '103091806397289', //place id - inserts a link to place fb page
'ticket_url' => 'url', //URL to buy a ticket for the event
'no_feed_story' => FALSE, //TRUE = dont display on page
'access_token' => 'token',
'picture' => '#'. realpath($file),
);
$createUrl = 'https://graph.facebook.com/page_id/events';
$test = fbcurl($createUrl, 'POST', $arrData); //Returns the event id
echo '<pre>';
print_r($test);
echo '</pre>';
function fbcurl($url, $method, $fields = array(), $auth = array()){
foreach($fields as $key => $value){
if(!is_string($value)){
$fields[$key] = json_encode($value);
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:multipart/form-data'));
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
// if(count($auth)===2)
// curl_setopt($ch, CURLOPT_USERPWD, $auth['user'].':'.$auth['pass']);
// }
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if(count($fields)>0){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields, null, '&'));
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
//curl_setopt($ch, CURLOPT_TIMEOUT, 60);
//curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php-3.2');
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "Content-Type: multipart/form-data");
//file_put_contents('fbcurllocal.txt', print_r(http_build_query($fields, null, '&'), true)."\n".$url);
$r = curl_exec($ch);
curl_close($ch);
if($r!=''){
$p = explode("\r\n\r\nHTTP/", $r);
$p = (count($p) > 1 ? 'HTTP/' : '') . array_pop($p);
list($h, $b) = explode("\r\n\r\n", $p, 2);
return array('header' => $h, 'body' => $b);
}else{
return array('header' => '', 'body' => 'error');
}
}

If you read the documentation, ther's no such field picture with \POST /{page-id}/events.
So, while creating an event you can not publish a cover picture along with it. So it's a two-step procedure-
Create event
API: \POST /{page-id}/events - Reference
This you've already done, just delete the picture parameter from there.
Upload cover picture
API: \POST /{event-id}/events - Reference
Give the image to the parameter: source
This way you can upload the cover pic to an event. Hope that helps.
Edit
The method to upload cover photo that I've mentioned will not work as of now (you can subscribe to this bug to get the update), instead you'll be needing a link to the image and make the call-
\POST /{event-id}
Parameter: cover_url
This works. I've tested!

look at the commtens before to understand it.
'asdf' => '#'. realpath($file).';type=image/jpeg',
(
[header] => HTTP/1.1 200 OK
x-fb-rev: 1200162
Access-Control-Allow-Origin: *
Cache-Control: private, no-cache, no-store, must-revalidate
Content-Type: application/json; charset=UTF-8
Date: Thu, 10 Apr 2014 14:26:20 GMT
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Pragma: no-cache
X-FB-Debug: nLKpXn4cbeF4AMa8kiOVMAfcKG5EFvQZmqvxFJXYC88=
Connection: keep-alive
Content-Length: 4
[body] => true
)
i tested this
'source' => '#'. realpath($file).';type=image/jpeg',
and this
'source' => '#'. realpath($file),
bevore

Related

MailChimp integration Error code 501 - Unsupported request - PHP

Below is my code to add contact to the mailchimp. I am getting mailchimp api key and list id from the env file in laravel. Also $data involves all the input.
$apiKey = env('MAILCHIMP_APIKEY');
$listId = env('MAILCHIMP_LIST_ID');
$auth = base64_encode( 'user:'.$apiKey);
$mailChimpdata = array(
'apikey' => $apiKey,
'email_address' => $data['email'],
'status' => $data['status'],
'merge_fields' => array(
'FNAME' => $data['firstname'],
'LNAME' => $data['lastname']
)
);
$json_data = json_encode($mailChimpdata);
$ch = curl_init();
$memberId = md5(strtolower($data['email']));
$url = 'https://us19.admin.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId;
curl_setopt($ch, CURLOPT_URL, $url);
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_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
But I am getting this below error
HTTP/1.0 501 Not Implemented
Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 334
Expires: Wed, 07 Apr 2021 08:49:45 GMT
Date: Wed, 07 Apr 2021 08:49:45 GMT
Connection: close
Set-Cookie: _abck=471ED28A26F6757CE57AC9E4318E7BD6~-1~YAAQHdcLF/keKqt4AQAASPOFqwWS/EeAKXso+2igviRTfIMw3pqbDMndhfZPfoX+eQn3Iv8iqfbogtNFmkU4lRDRQLdAWAYBhq2oOhDTiK ▶
Set-Cookie: bm_sz=5CBD698E96AC9E2B62898990FF45E196~YAAQHdcLF/oeKqt4AQAASPOFqwsLVm8fKmxtToeWU99vCUWske+XMuRtEjiCpzXhPE5xe8jGoh6EfYe8WG6zjMaaI2jPZos0gb2ER9jykveOE ▶
<HTML><HEAD>
<TITLE>Unsupported Request</TITLE>
</HEAD><BODY>
<H1>Unsupported Request</H1>
PUT to http://us19.admin.mailchimp.com/3.0/lists/applevendor/members/b642b4217b34b1e8d3bd915fc65c4452 not supported.<P>
Reference #8.1dd70b17.1617785385.68d39e
</BODY></HTML>
Please help me out to resolve this issue.
Based on Mailchimp documentation they use URL to add contact
"https://us20.api.mailchimp.com/3.0/lists/$list_id/members/"
but you have
"https://us19.admin.mailchimp.com/3.0/lists/$listId/members/$memberId"
with $memberId. I guess that id is their internal identificator.
I don't know if this topic is still relevant but here is my method using php / Guzzle:
$client = new Client();
$client->request('PUT', 'https://us9.api.mailchimp.com/3.0/lists/{list_id}/members/{subscriber_hash},
[
'json' => [
'email_address' => $email,
'status_if_new' => "unsubscribed",
],
'headers' => [
"Authorization" => "Basic YOUR_API_KEY"
]
]);
subscriber_hash => hash MD5

How to update user profile picture in QuickBlox using php api?

How to update user profile picture in Quickblox using php codeigntier ?
Documentation found at
http://quickblox.com/developers/Users
after found rest api i have found the solution for how to upload profile picture to quickblox user.
there are three 3 steps for uploading content as per quickblox rest api
First you generate token from the quickblox and then perform these 3 steps
Create file
https://quickblox.com/developers/Content#Create_a_file
$strFilename = '2.jpeg';
$post_body = http_build_query(array(
'blob[content_type]' => 'image/jpeg',
'blob[name]' =>$strFilename,
));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, QB_API_ENDPOINT.'blobs.json');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/x-www-form-urlencoded',
'QuickBlox-REST-API-Version: 0.1.0',
'QB-Token: ' . $token
));
$response = curl_exec($curl);
$error = curl_error($curl);
if ($response) {
return $response;
} else {
return false;
}
curl_close($curl);
After this curl call you have got the response and in response got the response like this
[blob] => Array
(
[id] => 7178102
[uid] => f9cc9d7938c4468f8bdccdcb68fb5d8c00
[content_type] => image/jpeg
[name] => 2.jpeg
[size] =>
[created_at] => 2017-02-07T10:35:38Z
[updated_at] => 2017-02-07T10:35:38Z
[ref_count] => 1
[blob_status] =>
[set_completed_at] =>
[public] => 1
[last_read_access_ts] =>
[lifetime] => 8600
[account_id] => 56721
[app_id] =>
[blob_object_access] => Array
(
[id] => 7178102
[blob_id] => 7178102
[expires] => 2017-02-07T11:35:38Z
[object_access_type] => Write
[params] => https://qbprod.s3.amazonaws.com/?Content-Type=image%2Fjpeg&Expires=Tue%2C%2007%20Feb%202017%2011%3A35%3A38%20GMT&acl=public-read&key=f9cc9d7938c4468f8bdccdcb68fb5d8c00&policy=eyJleHBpcmF0aW9uIjoiMjAxNy0wMi0wN1QxMTozNTozOFoiLCJjb25kaXRpb25zIjpbeyJidWNrZXQiOiJxYnByb2QifSx7ImFjbCI6InB1YmxpYy1yZWFkIn0seyJDb250ZW50LVR5cGUiOiJpbWFnZS9qcGVnIn0seyJzdWNjZXNzX2FjdGlvbl9zdGF0dXMiOiIyMDEifSx7IkV4cGlyZXMiOiJUdWUsIDA3IEZlYiAyMDE3IDExOjM1OjM4IEdNVCJ9LHsia2V5IjoiZjljYzlkNzkzOGM0NDY4ZjhiZGNjZGNiNjhmYjVkOGMwMCJ9LHsieC1hbXotY3JlZGVudGlhbCI6IkFLSUFJWTdLRk0yM1hHWEo3UjdBLzIwMTcwMjA3L3VzLWVhc3QtMS9zMy9hd3M0X3JlcXVlc3QifSx7IngtYW16LWFsZ29yaXRobSI6IkFXUzQtSE1BQy1TSEEyNTYifSx7IngtYW16LWRhdGUiOiIyMDE3MDIwN1QxMDM1MzhaIn1dfQ%3D%3D&success_action_status=201&x-amz-algorithm=AWS4-HMAC-SHA256&x-amz-credential=AKIAIY7KFM23XGXJ7R7A%2F20170207%2Fus-east-1%2Fs3%2Faws4_request&x-amz-date=20170207T103538Z&x-amz-signature=5e236c3da60a922951c8ab6281ae82af3a88e37c15d8630ad6ff590610a87fd8
)
)
Upload file
so you have to used the params url parameters and make another call for uploading file
$strFilename = '2.jpeg';
$url = 'https://qbprod.s3.amazonaws.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'Content-Type' => $arr['Content-Type'],
'Expires'=>$arr['Expires'],
'acl'=>$arr['acl'],
'key'=>$arr['key'],
'policy'=>$arr['policy'],
'success_action_status'=>$arr['success_action_status'],
'x-amz-algorithm'=>$arr['x-amz-algorithm'],
'x-amz-credential'=>$arr['x-amz-credential'],
'x-amz-date'=>$arr['x-amz-date'],
'x-amz-signature'=>$arr['x-amz-signature'],
'file' => new CurlFile('2.jpeg', $arr['Content-Type'], $strFilename)
));
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 204) {
echo 'Success!';
} else {
$error = substr($response, strpos($response, '<Code>') + 6);
echo substr($error, 0, strpos($error, '</Code>'));
}
return $response;
so by using this code your content file will be uploaded and you got the location of the file and used as profile picture or etc .
Declare file
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.quickblox.com/blobs/" . $strId . "/complete.xml"); // strId is blod id return by 1 step
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "blob[size]=10000"); //your file size
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "Quickblox-Rest-Api-Version: 0.1.0";
$headers[] = "Qb-Token: " . $token;
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
Please let me know the any issue regards the same.
Thanks

How to use ConvertAPI Word2Pdf?

I'm trying to call ConvertAPI.com/Word2Pdf, but with no success.
My sample code is:
$fileToConvert = 'test.docx';
$apiKey = *******;
$postdata = array('OutputFileName' => 'test.pdf', 'ApiKey' => $apiKey, 'File' => $fileToConvert);
$ch = curl_init("http://do.convertapi.com/Word2Pdf");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
var_dump($result);
I'm getting as a result something like: "HTTP/1.1 100 Continue HTTP/1.1 400 The 'File' parameter cannot be null. Please set value. Cache-Control: no-cache, no-store Pragma: no-cache Content-Type: text/html..."
What I'm doing wrong here?
Try this using http://unirest.io/php.html library
$response = Unirest::post(
"http://do.convertapi.com/Word2Pdf?ApiKey=<Your api key>",
array(
"File" => "#/tmp/file.path",
"OutputFormat" => "pdf",
)
);

Content-type not changing with CURLOPT_HTTPHEADERS

I'm trying to POST some JSON to a web service with cURL, using the following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://index.yolink.com/index/define?o=json&ak=APIKEY');
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));
$data = array(
'ignore-robots' => 'false',
'language' => 'english',
'crawl-delay' => '0',
'depth' => '3',
'root' => array('url' => 'http://bartleby.com/')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result=curl_exec($ch);
var_dump($result);
I get the following in return:
string(282) "HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Access-Control-Allow-Origin: * Content-Type: text/plain Content-Length: 120 Date: Fri, 18 Mar 2011 19:03:23 GMT {"code":"error.indexdefinition.invalid","message":"Invalid content provided for /define. Error:Premature end of file.."}"
I found this blog post that seems to be related -- it does seem to be sending text/plain even though I've specified the ContentType in CURLOPT_HTTPHEADERS as application/json. But adding http_build_query hasn't helped.
Thanks in advance for any help!
I believe it should be HTTPHEADER, not HTTPHEADERS.
http://php.net/manual/en/function.curl-setopt.php

how to pass data to JSON server using cURL with POST [duplicate]

I'm trying to POST some JSON to a web service with cURL, using the following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'http://index.yolink.com/index/define?o=json&ak=APIKEY');
curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));
$data = array(
'ignore-robots' => 'false',
'language' => 'english',
'crawl-delay' => '0',
'depth' => '3',
'root' => array('url' => 'http://bartleby.com/')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result=curl_exec($ch);
var_dump($result);
I get the following in return:
string(282) "HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Access-Control-Allow-Origin: * Content-Type: text/plain Content-Length: 120 Date: Fri, 18 Mar 2011 19:03:23 GMT {"code":"error.indexdefinition.invalid","message":"Invalid content provided for /define. Error:Premature end of file.."}"
I found this blog post that seems to be related -- it does seem to be sending text/plain even though I've specified the ContentType in CURLOPT_HTTPHEADERS as application/json. But adding http_build_query hasn't helped.
Thanks in advance for any help!
I believe it should be HTTPHEADER, not HTTPHEADERS.
http://php.net/manual/en/function.curl-setopt.php

Categories