使用LinkedIn Vector Asset API上传二进制图片返回null的问题求助
Let's walk through why your PHP curl request to upload an image to LinkedIn is returning null, and fix the issues step by step.
Key Issues in Your Current Code
1. Wrong Content-Type Header
You’re sending Content-Type: text/plain for a JPG image—this is a critical mismatch. LinkedIn expects the correct media type for your file; for JPEGs, that’s image/jpeg. Using an incorrect Content-Type can cause the API to reject your request silently, leading to a null response.
2. No Curl Error Handling
When curl_exec() returns null, it typically means the curl request itself failed (not just an API-level error). Without checking for curl errors, you’re flying blind—common culprits include SSL certificate issues, network timeouts, or a malformed upload URL.
3. (Optional) Binary Data Handling
While PHP 5.6+ automatically handles binary data in CURLOPT_POSTFIELDS, older versions may require explicitly enabling CURLOPT_BINARYTRANSFER. Adding this won’t hurt even if you’re on a newer version.
Fixed Code with Debugging
Here’s the revised code that addresses these issues, plus added debugging to help you diagnose future problems:
$imagePath = 'C:/Users/HH/Desktop/test.jpg'; $body = file_get_contents($imagePath); // First, make sure we could read the image file if ($body === false) { die("Error: Could not read image file at $imagePath"); } $uploadUrl = "https://api.linkedin.com/mediaUpload/C4D22AQFyt9bYpBuRcw/feedshare-uploadedImage/0?ca=vector_feedshare&cn=uploads&m=AQIUeWqFh419_gAAAXyYlBzYF8LNZVjHdjGaH103jP998K1bpxeJPGGJtw&app=82730583&sync=1&v=beta&ut=1ccVodv_trJFY1"; $ch = curl_init($uploadUrl); // Configure curl options curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // Set the correct Content-Type for JPEG images curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: image/jpeg', 'Authorization: Bearer ' . $token ]); // Only uncomment the line below if you're testing with self-signed certs (not for production!) // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); // Check for curl-level errors first if (curl_errno($ch)) { var_dump("Curl Error: " . curl_error($ch)); } else { var_dump($result); } curl_close($ch);
Additional Things to Verify
- Token Permissions: Ensure your access token has the
w_member_socialscope—this is required for uploading media to LinkedIn feeds. - Upload URL Validity: LinkedIn’s upload URLs are temporary and expire quickly. Make sure you’re using a freshly generated URL from the initial asset registration step.
- Image Requirements: Confirm your image meets LinkedIn’s specs: max file size (10MB for most use cases), supported formats (JPG, PNG), and appropriate dimensions.
内容的提问来源于stack exchange,提问作者Hoda Kh

