Bug Subscription instagram API with cURL - php

Here is my code to subscribe/unsubscribe to an real time of Instagram API.
The unsubscribe works but not the subscribe. Im not really good with cUrl, I just have use this part of code from http://thegregthompson.com/instagram-real-time-api-php/ with some extra customization.
<?php
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
if (isset($_GET['unsubscribe']))
{
echo "unsubscribe";
$url .= "?client_id=" . $config['instagram']['client_id'] . "&client_secret=" .
$config['instagram']['client_secret'] . "&id=" . $_GET['tag'];
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
else
{
$attachment = array(
'client_id' => $config['instagram']['client_id'],
'client_secret' => $config['instagram']['client_secret'],
'object' => 'tag',
'object_id' => $_GET['tag'],
'aspect' => 'media',
'verify_token' => $config['instagram']['verify_token'],
'callback_url'=> 'http://' . $_SERVER['HTTP_HOST'] . '/callback/endpoint.php'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_POST, true);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
print_r($result);
?>
$config is correctly included.
I spent so much time to try to fix it but no way.
Can you please help me to make the subscription works ?
Ask me if you need more details.
Thanks a lot

Ok, I fixed it: I think we should set the CURLOPT_POST before CURLOPT_POSTFIELDS
Correct
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
Incorrect
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_POST, true);

Related

PHP Jira Service Desk REST API

I am struggling to create a request on Jira Service Desk in PHP.
My code is :
public function reportIssue(Request $request) {
//post
//authenticate to Jira ...
//create request ...
//response ....
//do something afterwards ... post ...
$jdata = json_encode($request);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_POST => 1,
CURLOPT_URL => SERVICE_DESK_URL . '/rest/servicedeskapi/request/' . $request,
CURLOPT_USERPWD => SERVICE_USERNAME . ':' . SERVICE_PASSWORD,
CURLOPT_POSTFIELDS => $jdata,
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => true
));
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result);
}
I am getting empty response body, still like something is wrong.
Pardon me if my mistake is obvious.
Had the same issue. Here is my working curl config:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, SERVICE_DESK_URL . '/rest/servicedeskapi/request/' . $request);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, SERVICE_USERNAME . ':' . SERVICE_PASSWORD);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json;charset=UTF-8",
'X-ExperimentalApi: opt-in'
)
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($jdata));
$response = curl_exec($curl);

Make a wp_remote_post when not logged in

Hi i'm developing a plugin which adds a new field to the order (WooCommerce). The field needs to make an ajax request to a file in my plugin, that file then needs to make a cURL request to another website (or wp_remote_post). But i'm experiencing difficulties when making the request.
I can't get the ordinary cURL to work nor the wp_remote_post function.
Here's a snippet of the cURL in my file which the ajax requests to.
<?php
$shipping_place = array(
'country_code' => $country_code,
'postcode' => $postcode,
'street' => $street,
'number_of_droppoints' => $number_of_droppoints
);
$auth = array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode('user:password')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $shipping_place);
$result = curl_exec($ch);
if(curl_errno($ch)){
$msg = 'Curl error: ' . curl_error($ch);
} else {
$result = json_decode($result['body']);
if ( $result->status == 'error' ) {
echo $result;
}
pred($result);
echo $result->result;
}
curl_close ($ch);
?>
Solved: I had to localize the wp-admin script.
That would use the wp function like this
<?php wp_localize_script( $handle, $name, $data ); ?>
Reference here.

Creating Leads in SalesForce using REST API in PHP

I have been trying to create a lead from SalesForce's REST API for the past several days but I can't for the life of me get it working. I am able to get the access token no problem but from there on as far as creating a lead I am having absolutely no luck at all.
I keep seeing in all of the documentation this:
curl https://na1.salesforce.com/services/data/v20.0/sobjects/Account/ -H "Authorization: Bearer token -H "Content-Type: application/json" -d #newaccount.json"
How would I do this in PHP's curl though? I have tried and tried but had no luck at all.
Here is how I have got the access token:
$ch = curl_init();
// set URL options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://login.salesforce.com/services/oauth2/token?grant_type=password&client_id=".CONSUMER_KEY."&client_secret=".CONSUMER_SECRET."&username=".USERNAME."&password=".USERPASS.SECURITY_TOKEN);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab HTML
$data = curl_exec($ch);
$data = json_decode($data, true);
$code = $data['access_token'];
curl_close($ch);
I have tried doing something like this after this code, however I have had no luck.
$token_url = LOGIN_BASE_URL.'/services/oauth2/token';
$post_fields = array(
'code' => $code,
'grant_type' => 'authorization_code',
'client_id' => CONSUMER_KEY,
'client_secret' => CONSUMER_SECRET,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$token_request_body = curl_exec($ch)
I just need to figure out how to create leads in SalesForce, I have no idea where to go from here. Any help would be greatly appreciated as I cannot find decent documentation anywhere that helps me.
create account demo, should get you started:
function create_account($name, $instance_url, $access_token) {
$url = "$instance_url/services/data/v20.0/sobjects/Account/";
$content = json_encode(array("Name" => $name));
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token",
"Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
echo "HTTP status $status creating account<br/><br/>";
curl_close($curl);
$response = json_decode($json_response, true);
$id = $response["id"];
echo "New record id $id<br/><br/>";
return $id;
}

POST to Facebook page via CURL

I've set up a script to post news to my facebook page via PHP
It worked for the last 2 years
Now, without notice it stopped working.
I correctly get the access_token but the second part returns this error
{"error":{"message":"An unknown error has occurred.","type":"OAuthException","code":1}}
Here is the code
$url = "https://graph.facebook.com/oauth/access_token";
$postString = "client_id=KEY&client_secret=SECRET&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$access_token = str_replace( "access_token=", "", curl_exec($curl) );
$titolo = 'Test';
$link_pulito = 'test.html';
$testo_fb = 'Test';
$attachment = array(
'access_token' => $access_token,
'message' => 'MESSAGE',
'name' => 'test',
'link' => 'http://www.test.com/workshop/',
'description' => 'test test test',
'picture'=>'http://www.test.com/77818763a19937bdd82b25f26cef2522.jpg'
);
// set the target url
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/MYPAGE/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); //to suppress the curl output
$result= curl_exec($ch);
curl_close ($ch);
Facebook has changed the way of leasing tokens.
Please use the facebook PHP SDK from https://github.com/facebook/facebook-php-sdk

curl POST error: The requested URL returned error: 413

I've looked around a bit and can't find a solution that solves my problem. I'm getting the following error: "The requested URL returned error: 413."
I'm posting some information via curl and PHP to a URL via HTTPS. I'm told that I have to pass the "Content-Length" along with my request since the destination is https and not http.
Here's the code I'm using:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$api = curl_exec($ch);
My strlen($data_string) command is returning a value of 5, which is much less than the actual length of the $data_string, which is much longer. I assume this is the problem unless someone thinks it might be being caused by something else.
Here's what I ended up with:
$user = '11111111111111111';
$books = array(111);
$data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books);
$data_string = array('json'=>json_encode($data));
$target_url = 'https://www.test.com/test.php'; // fake URL of course
$ch = curl_init();
if (!$ch) {
die("Couldn't initialize a cURL handle");
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$api = curl_exec($ch);
No SSL or size errors and it seems to work fine.

Categories