I am trying to upload the image to database using api
$curl = curl_init();
$headers = array("Content-Type: multipart/form-data",);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt_array($curl, array(
CURLOPT_URL => 'api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('file'=> new CURLFILE($_FILES['cfile']['tmp_name'])),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
In this code image is not getting store as expected CURLOPT_POSTFIELDS => array('file'=> new CURLFILE($_FILES['cfile']['tmp_name']))
Related
I'm using php curl API to fetch the posts from WordPress. But I'm not getting X-WP-Total or X-WP-TotalPages keys with response to work with pagination.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://example.com/wp-json/wp/v2/posts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
"Content-Type: application/json",
"Access-Control-Allow-Origin: *",
"Access-Control-Allow-Methods: GET, POST, OPTIONS",
"Access-Control-Allow-Headers: X-Requested-With"
],
));
$response = curl_exec($curl);
$response = json_decode($response);
// print_r($response);exit;
curl_close($curl);
I am trying to get product data from google merchant center (google shopping ), my curl command works fine on postman, but not on php. Please help me
On postman:
On Php:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://shoppingcontent.googleapis.com/content/v2.1/436306321/products',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ya29.a0ARrdaM8TK4rZeVRnsj9MgcFg3STE1EqGvwj20J-REq8f6poCzGIv2MzUIegdSP1DDa_-HQpHIQNBTU6Rnxf7HsnWCjwkP7_pwkp-kr5zm0o0Z6EuZCVa4IdwHzX7MGM8SQ-94Vkxps4JRSbkgplYo4w1K8lPwQ'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
There's a free weather API in my country that I want to use to get rain data. The request is simple https://apitempo.inmet.gov.br/estacao/2021-05-03/2021-05-06/A826 and there's no authentication. If I try the request using Postman, my browser or AJAX, it works fine and I get the results I need. However, using cURL, I get nothing.
Here's the PHP code that I tried:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://apitempo.inmet.gov.br/estacao/2021-05-03/2021-05-06/A826',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I have the cURL.dll installed and tested in another server, but it didn't work.
Is there a problem with the code or is it the API that blocks PHP requests?
its look like this website looking for user-agent header.
try this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://apitempo.inmet.gov.br/estacao/2021-05-03/2021-05-06/A826',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'User-Agent: something'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Am trying to get onedrive access token.
Here is the Request:
GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}
&response_type=token&redirect_uri={redirect_uri}
Here is Response:
https://myapp.com/auth-redirect#access_token=EwC...EB
&authentication_token=eyJ...3EM&token_type=bearer&expires_in=3600
&scope=onedrive.readwrite&user_id=3626...1d
I have implemented the two code below in other to get the access token but am having error below when page is being redirected for login.
AADSTS900144: The request body must contain the following parameter: 'client_id'.
Scope property is space seperated hence see onedrive documentation link
code 1
$data_string = array(
"client_id" => "myclient id",
"response_type" => "token",
"redirect_uri" => "http://localhost/test/callback.php"
"scope" => ['files.readwrite', 'offline_access'],
);
$data = json_encode($data_string);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "$data",
));
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
code 2
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=myclientid-goes-here&scope=files.readwrite.all offline_access
&response_type=token&redirect_uri=http://localhost/test/callback.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
//CURLOPT_POSTFIELDS => "$data",
));
$result = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
This was resolved using http_build_query() function
hence
$data = http_build_query($data_string, '', '&');
I am trying to upload an image using linkedin api.
I already uploaded image in my server and now I want to upload the upoaded image url to linkedin. But I got the error
{ "serviceErrorCode":0,"message":"The request is not a multipart
request","status":400 }
$curl1 = curl_init();
$m_url="https://api.linkedin.com/media/upload";
$curl_header = array("Authorization:Bearer ".$access_token);
$uploadRequest = array(
'name'=>'media',
'filename' => basename($medias[0]),
'media_data' => base64_encode(file_get_contents($medias[0]))
);
curl_setopt_array($curl1, array(
CURLOPT_URL => $m_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>$uploadRequest,
CURLOPT_HTTPHEADER=>$curl_header,
));
$response1 = curl_exec($curl1);
$err1 = curl_error($curl1);
curl_close($curl1);
This is the request. Please anyone tell me What is the exact issue and how to sort this out.Thanks in advance.
Added header like this:
$curl_header=array("Content-Type:multipart/form-data",
"Authorization:Bearer ".$access_token);
And changed the post params like this
$curl1 = curl_init();
$m_url="https://api.linkedin.com/media/upload";
$uploadRequest['file'] = new CURLFile($img, 'image/png', basename($img));
$curl_header=array("Content-Type:multipart/form-data", "Authorization:Bearer ".$access_token);
curl_setopt_array($curl1, array(
CURLOPT_URL => $m_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>$uploadRequest,
CURLOPT_HTTPHEADER=>$curl_header,
));
$response1 = curl_exec($curl1);
$err1 = curl_error($curl1);
$status1 = curl_getinfo($curl1, CURLINFO_HTTP_CODE);
curl_close($curl1);