Post a link in VIADEO using it API - php

I have tried to post a link in viadeo net work using it API, i have used the following CURL code. but its not working, I have tried with php as my server side code. Can Any one please help me to sort out this.
API reference
$url = 'https://partners.viadeo.com/api/member/activity/share';
$headers[] = 'Authorization: Bearer ' . $userArray['accessToken'];
$headers[] = "Accept: language/json";
$headers[] = 'Content-Type: application/JSON';
$headers[] = 'X-CSRF-Token: ' . $token;
$fields = array(
'status' => "My Messafge",
'url' => "www.google.com"
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
But I got response as below
HTTP/1.1 406 Not Acceptable
Cache-control: no-cache="set-cookie"
Content-Type: application/json; charset=utf-8
Date: Wed, 10 Aug 2016 09:20:16 GMT
ETag: W/"79-LjiXR3TEaswtDc0NbPbnlA"
Server: nginx/1.8.1
Set-Cookie: AWSELB=EB652D71104D5A540BBD90F6B05BD98BEE3F41DAFDC50D85BE8FBFFCF095D7D78EB45C92813C3646048981782B8C4C76C884420B93A5B55954F394AA61798FEFC028CD93F8;PATH=/
X-Powered-By: Express
Content-Length: 121
Connection: keep-alive
{"error":"The provided Accept header is not acceptable. Try one of the following : application/json,multipart/form-data"}

Try Like this, it will work for sure
$headers[] = "Accept: application/json";
$headers[] = 'Content-Type: application/json';

Related

PHP cURL not sending headers (?)

I attempted to modify the code I found on this webpage: https://tutorialsclass.com/php-rest-api-get-data-using-curl/
The API I'm using needs two custom headers: Accept-Encodig and x-api-key.
My modified code looks like this:
$curl_handle = curl_init();
$url = "http://thelinktomyAPI";
$headers = array(
'Accept-Encoding: application/json',
'Content-Type: application/json',
'x-api-key: MyLongAPIKey'
);
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_ENCODING, 'application/json');
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_data = curl_exec($curl_handle);
if ($curl_data === false)
{
print_r('Curl error: ' . curl_error($curl_handle));
}
curl_close($curl_handle);
print_r($curl_data);
So as you can see I'm making my headers visible to make sure I'm actually sending them. What I get shown is a webpage with the following:
HTTP/1.1 302 Moved Temporarily Date: Thu, 02 Dec 2021 08:09:59 GMT
Content-Type: text/html Content-Length: 110
Connection: keep-alive
Location: http://thelinktomyAPI
X-Trace-ID:f306fe34-079b-493c-b5db-073e792ec2a1
X-Kong-Response-Latency: 0
Server: kong/2.3.3 302 Found
Since the Content-Type is shown as "text/html" it leads me to believe that my headers aren't sent at all, hence I'm not getting any data. I'd appreciate any help.
Edit Adding curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); as Professor Abronsius suggested helped!

How to get only the body of the response in a Curl request in php

I'm a beginner in Curl and I'm having issues with the value that I get from curl_exec().
I'm connecting to a bank's server and want to get only the array from the server, but curl_exec() returns the whole string with the header and the array. How can I make it only send the array?
<?php
$url = 'https://the-url.com';
$auth = 'authentication key';
$method ='POST';
$grant = 'grant_type=client_credentials';
$crl = curl_init();
$headr = array();
$headr[] = 'Content-type: application/x-www-form-urlencoded';
$headr[] = 'Authorization: Basic '.$auth;
curl_setopt($crl, CURLOPT_URL,$url);
curl_setopt($crl, CURLOPT_HTTPHEADER,$headr);
curl_setopt($crl, CURLOPT_POST,true);
curl_setopt($crl, CURLOPT_HEADER, true);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($crl, CURLOPT_POSTFIELDS, $grant);
$rest = curl_exec($crl);
if ($rest === false)
{
print_r('Curl error: ' . curl_error($crl));
}
curl_close($crl);
$data = json_decode($rest, true);
echo $rest;
?>
this outputs
HTTP/2 200 a: x content-type: application/json content-length: 345 date: Tue, 21 Sep 2021 12:26:47 GMT strict-transport-security: max-age=63072000{array}
when I want it to output just {array} so I can use json_decode() on it
Try changing this line
$headr[] = 'Content-type: application/x-www-form-urlencoded';
to
$headr[] = 'Content-type: application/json';
Since you are expecting a json response.

PUT upload php curl command response is "Missing Content-Length header."

I write a php script to upload a media file but when I use it the response occurs:
Missing Content-Length header.
The php code is:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://raz-ul.domenka.com/v1/AUTH_8a619275-f933-4da1-b289-0ca1a2a3a4a5/bcd/drop1.avi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Token: AUTH_tkd78bedda5bec4613b80becb1b2b3b4b5";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
var_dump($header);
On the other hand When I do something like that in cli I succeed:
kinduser#43915-1-c1c2c3-01:~/public_html/adam$ curl -i -X PUT -H "X-Auth-Token: AUTH_tkd78bedda5bec4613b80bec0123456789" -T drop.avi https://raz-ul.domenka.com/v1/AUTH_8a619275-f933-4da1-b289-0ca1a2a3a4a5/bcd/drop.avi
HTTP/1.1 100 Continue
HTTP/1.1 201 Created
Last-Modified: Thu, 16 Feb 2017 10:12:54 GMT
Content-Length: 0
Etag: fa60ba1b78299bfae5ac61d1d2d3d4d5
Content-Type: text/html; charset=UTF-8
X-Trans-Id: tx114b22027223415c850db-e1e2e3e4e5
Date: Thu, 16 Feb 2017 10:12:53 GMT
kinduser#43915-1-c1c2c3-01:~/public_html/adam$
But I would like to do this in php. How to fix my php code?
Update
I added some code to tell curl which file to send. It sent the file but the file changed on the server. It got a bigger weight from 660kB to 660.31kB. After downloading to my pc it got impossible to open via video player.
That code is like this now:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://raz-ul.domenka.com/v1/AUTH_8a619275-f933-4da1-b289-0ca1a2a3a4a5/bcd/drop7.avi");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$headers = array();
$headers[] = "X-Auth-Token: AUTH_tkd78bedda5bec4613b80becb1b2b3b4b5";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$curlFile = curl_file_create ('./drop.avi');
$post = array (
'extra_info' => '123456',
'file_contents' => $curlFile
);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$header_size = $info['header_size'];
$header = substr($response, 0, $header_size);
var_dump($header);
Any idea how to fix it?

PHP curl is not parsing POST data

So i am trying to send a HTTPS POST request to a website but curl is not setting my post data nor my headers.
Here is my code(i changed the domain and values):
<?php
//Create connection
$ch = curl_init();
//Set the headers
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
//Set the POST data
$headers = array();
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: website.com";
$headers[] = "Content-Type: application/x-www-form-urlencoded";
$headers[] = "Something: Something_Data";
// Configure the request (HTTPS)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, "https://website.com/test");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MyUserAgentHere");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, 1); //just to be sure...
//Set the POST data and Headers
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("DATA1=VAL1&DATA2=VAL2"));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result=curl_exec($ch);
echo $result;
curl_close($ch);
?>
So basically i'm trying to send a HTTPS POST request to https://website.com/test". The request is sent and i get an answer but for some reasons it doesn't parse the POST data nor the Headers.
Here is my request in fiddler:
POST https://website.com/test HTTP/1.1
Host: website.com
Here is the answer from the request:
HTTP/1.1 400 Bad Request
Date: Fri, 06 Nov 2015 00:57:15 GMT
Server: Apache
Cache-Control: no-store
Pragma: no-cache
Connection: close
Content-Type: application/json;charset=UTF-8
Content-Length: 158
{"error":"unsupported DATA1"}
As you can see, the POST data and the headers are not set in my request for some reason. I have no idea why curl is not setting my headers and my post fields.
Infos: I am using XAMPP with php5.
You shouldn't URL-encode the entire parameter string, that will encode the = and & so they're no longer delimiters. You should just encode the values.
curl_setopt($ch, CURLOPT_POSTFIELDS, 'DATA1=' . urlencode($val1) . '&DATA2' . urlencode($val2));
You can use an array instead, and cURL will do the encoding for you.
curl_setopt($ch, CURLOPT_POSTFIELDS, array('DATA1' => $val1, 'DATA2' => $val2));
If you use the array mechanism, remove the Content-type: application/x-www-form-urlencoded header, because cURL sends it in multipart/form-data format.

How to convert response header in array in php

I am using Gcm for sending notifications and I have used curl for the same. Here is the script
$registrationId= array('adkbvkasdjb');
$headers=array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$sendData=array(
'msg'=>'any message');
$url= GCM_URL;
$details=array(
'registration_ids'=>$registrationId,
'time_to_live'=> 48*60*60,
'data'=>$sendData,
);
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 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($details));
$result= curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
// closing the curl connection
curl_close($ch);
Now i am getting the header of the response as
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Thu, 16 Jan 2014 11:05:17 GMT
Expires: Thu, 16 Jan 2014 11:05:17 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked
Since I am not very good with regex and i searched a lot about a method that could parse the header of the response but all in vain.
Question:-
So i just wanna know is there anything or any method that could parse the header response as an array ?
Note:- I found a little about http_parse_header() but it need pcre to be installed so i am not sure whether it is installed on my REAL SERVER :)
You don't need any Regex to do it. If you read the first comment of parse_http_headers documentation on php.net, there is a little implementation if pecl_http lib is not found.
Follow the link:
http://www.php.net/manual/en/function.http-parse-headers.php#112986

Categories