I am able to "GET" the activity stream but not "POST" to it. It seems its a technical error.
This is the code which works for getting the activity stream:
function getActivityStream()
{
$as=$this->request('https://www.yammer.com/api/v1/streams/activities.json');
var_dump($as);
}
function request($url, $data = array())
{
if (empty($this->oatoken)) $this->getAccessToken();
$headers = array();
$headers[] = "Authorization: Bearer " . $this->oatoken['token'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . '?' . http_build_query($data) );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
return json_decode($output);
}
ok so that works fine...
the code below, returns a Yammer "oops this page could not be found" message:
function putActivityStream()
{
$data=array('type'=>'text', 'text'=>'hello from api test call');
$json=json_encode($data);
$res=$this->post('streams/activites.json',$json);
}
function post($resource, $data)
{
if (empty($this->oatoken)) $this->getAccessToken();
$ch = curl_init();
$headers = array();
$headers[] = "Authorization: Bearer " . $this->oatoken['token'];
$headers[]='Content-Type: application/json';
$url = 'https://www.yammer.com/api/v1/' . $resource;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
return $response;
}
One of the examples from:
http://developer.yammer.com/api/streams.html
POST https://www.yammer.com/api/v1/streams/activities.json
Requests must be content-type: application/json.
{
"type": "text",
"text": "The build is broken."
}
You're going to kick yourself. You have a typo in your code. :)
Change:
$res=$this->post('streams/activites.json',$json);
to
$res=$this->post('streams/activities.json',$json);
Simples.
Related
I have been trying to create a php function to call PDF extract API. I always got the following error:
{"requestId":"IEt94LRYdvDw4Xd9rzzcTqYt4dwWa1Sc","type":"Bad Request","title":"
Not a multipart request.
Aborting.","status":400,"report":"{\"error_code\":\"INVALID_MULTIPART_REQUEST\"}"}
My code is below:
$cfile = curl_file_create($pdf_file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://cpf-ue1.adobe.io/ops/:create");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("fileInput1"=>$cfile,"contentAnalyzerRequests"=>"{ \"cpf:engine\": { \"repo:assetId\": \"urn:aaid:cpf:58af6e2c-1f0c-400d-9188-078000185695\" }, \"cpf:inputs\": { \"documentIn\": { \"cpf:location\": \"fileInput1\", \"dc:format\": \"application/pdf\" }, \"params\": { \"cpf:inline\": { \"elementsToExtract\": [ \"text\", \"tables\" ], \"renditionsToExtract\": [ \"tables\", \"figures\"] } } }, \"cpf:outputs\": { \"elementsInfo\": { \"cpf:location\": \"jsonoutput\", \"dc:format\": \"application/json\" }, \"elementsRenditions\": { \"cpf:location\": \"fileoutpart\", \"dc:format\": \"text/directory\" } } }"));
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Accept: application/json, text/plain";
$headers[] = "Authorization: Bearer $token";
$headers[] = "x-api-key: $client_id";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
Can anybody help me?
I have this code implemented inside Laravel.
$ch = curl_init();
$link = "https://link";
$token = "";
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Accept: application/json",
"Authorization: Bearer {$token}",
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($curl, $header) use (&$responseHeaders) {
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2)
return $len;
$responseHeaders[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
});
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
It takes time to download https://link and then I got a $response value of
{
"Message": "X-Cluster download failed",
"Exception": null
}
From the return message I have no Idea what is
X-Cluster download failed
means.
I take to research but couldn't find a direct answer.
Any Idea?
Edit: https://link is valid link that represented tha original one it's a sensitive and private, the reason why I pasted like that.
I have a web service that I am working with and using PHP/cURL to connect with. When I use postman I get
"ShippingInfo": {
"<ShippingMethodId>k__BackingField": 0,
"<ShippingMethod>k__BackingField": null,
"<ShippingNote>k__BackingField": null,
"<ShippingProvider>k__BackingField": null
},
which is correct but when I dump the values in my Drupal site, I get
"ShippingInfo":{"k__BackingField":0,"k__BackingField":null,"k__BackingField":null,"k__BackingField":null}
It seems to be trimming off the <Shipping*> from the response. Any ideas on why this might be happening and how to make it stop?
Here is my cURL call:
ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$headers = array();
$headers[] = 'tokenvalidate: ' . $this->TOKEN;
$headers[] = 'Content-Type: text/json; charset=UTF-8';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
watchdog('cURL error', curl_errno($ch));
} else {
return $result;
}
curl_close($ch);
This seems to be the only object that is having the issue. All other calls are fine.
why some url with json content return null on php function get page content?
i used these ways :
file_get_content
curl
http_get
but return null . but when page open with browser json content show on browser ?? anyone can help me?
$url = 'https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=9IaRpdmQzFppJMabLHbHyzBaQnm8bM';
$result = file_get_content($url);
return null but in browser show
{"error_description": "Access token has expired.", "error": "invalid_credentials"}
It returns the following:
failed to open stream: HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
So, you have to be authorized.
You ca play with this code snippet (I can't test since token expired again):
<?php
$access_token = "s9spZax2Xrj5g5bFZMJZShbMrEVjIo"; // use your actual token
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/";
// Solution with file_get_contents
// $content = file_get_contents($url . "?access_token=" . $access_token);
// echo $content;
// Solution with CURL
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Basic " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url); // . $params - if you need some params
curl_setopt($ch, CURLOPT_POST, false);
// curl_setopt($ch, CURLOPT_USERPWD, $userPwd);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($ch_error) {
throw new Exception("cURL Error: " . $ch_error);
}
if ($info["http_code"] !== 200) {
throw new Exception("HTTP/1.1 " . $info["http_code"]);
}
echo $result;
I can't make it work.. :( I have this function (for create passthrough transcoder), when I run I see NULL in the web. If I test directly from the browser with the url, it does notify me that there is a problem an auth (apikey and acceskey)
function createPassthrough($name, $source_url, $recording = null)
{
$url = "https://sandbox.cloud.wowza.com/api/v1/transcoders";
$json = '{
"transcoder":{
"billing_mode":"pay_as_you_go",
"broadcast_location":"eu_belgium",
"delivery_method":"pull",
"name":"prueba",
"protocol":"rtsp",
"source_url":"url_camara",
"transcoder_Type":"passthrough",
"low_latency":true,
"buffer_size":0,
"play_maximum_connections":100,
"stream_smoother":false
}
}';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json; charset=utf-8',
'Content-Type: application/json; charset=utf-8',
'wsc-api-key:' . $apiKey,
'wsc-access-key:' . $accessKey,
));
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
var_dump($obj);
}
What am I doing wrong? Thanks in advance.
You should check for curl errors after $result = curl_exec($ch);.
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}